Chain of Responsibility

From CSSEMediaWiki
Revision as of 03:43, 16 July 2009 by Michal Connole (Talk | contribs)
Jump to: navigation, search

(This is summarised from GoF design patterns book)

Contents

Intent

Avoid coupling between the request sender and receiver by introducing more than one objects to handle the request.

When to use it (Applicability)

  • If there is more than one objects could handle the request, and handler isn't known until run-time
  • If a request needs to be issued to several objects without specify explicit receiver
  • The handlers for the request only be specified at run-time (dynamically)

How it works (Structure)

Structure - Chain of Responsibility.jpg

  • Handler
    • defines the interface for request handling
    • implement the successor link (optional)
  • ConcreteHandler
    • handles the actual request
    • provide an access to its successor
    • handle the request if it can, otherwise the request is forwarded to the successor
  • Client
    • initiates the request

Benefits and Drawbacks (Consequences)

  • Reduced coupling. Since no explicit handles are specified, the coupling between client and request hanlder is hugely reduced.
  • Adding more flexibility on object responsiblity assigning. Flexiblity in distributing responsibility is increased. By using different handler objects, the handling of the request can be assigned to different object (handler) at run time, which could also give different behaviours.
  • (Drawback) Request is not guaranteed to be handled.
Personal tools