Chain Of Responsibility (Pipeline) Design Pattern

Chain of responsibility design pattern image

Definition

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. (dofactory)


  • Handler defines an interface for handling the requests and can implement the successor link (optional).
  • ConcreteHandler handles requests it is responsible for and can access its successor. if the ConcreteHandler can handle the request, it does so; otherwise it forwards the request to its successor.
  • Client initiates the request to a ConcreteHandler object on the chain.

  • The chaining mechanism uses recursive composition to allow an unlimited number of handlers to be linked.
  • If the “current” object is not available or sufficient, then it delegates to the base class, which delegates to the “next” object, and the circle of life continues.
  • Multiple handlers could contribute to the handling of each request. The request can be passed down the entire length of the chain, with the last link being careful not to delegate to a “null next”.

Applicability
  • More than one object can handle a command
  • The handler is not known in advance
  • The handler should be determined automatically
  • It’s wished that the request is addressed to a group of objects without explicitly specifying its receiver
  • The group of objects that may handle the command must be specified in a dynamic way
  • Note: The pattern is used in windows systems to handle events generated from the keyboard or mouse. Exception handling systems also implement this pattern, with the runtime checking if a handler is provided for the exception through the call stack. If no handler is defined, the exception will cause a crash in the program, as it is unhandled. .Net framework implements Chain Of Responsibility design pattern for HttpModule. In JavaEE, the concept of Servlet filters implement the Chain of Responsibility pattern, and may also decorate the request to add extra information before the request is handled by a servlet.

Consequences
  • Launch-and-leave requests with a single processing pipeline that contains many possible handlers allowing you to add or remove responsibilities dynamically by changing the members or order of the chain.
  • An object-oriented linked list with recursive traversal.
  • Chain of Responsibility simplifies object interconnections. Instead of senders and receivers maintaining references to all candidate receivers, each sender keeps a single reference to the head of the chain, and each receiver keeps a single reference to its immediate successor in the chain. So, sender will not know which object in the chain will serve its request.

Downsides
  • Unfortunately, the Chain doesn't guarantee that every command is handled, which makes the problem worse, since unhandled commands propagate through the full length of the chain, slowing down the application. One way to solve this is by checking if, at the end of the chain, the request has been handled at least once, otherwise we will have to implement handlers for all the possible requests that may appear.
  • Broken Chain problem: Sometimes we could forget to include in the implementation of the handleRequest method the call to the successor, causing a break in the chain. The request is not sent forward from the broken link and so it ends up unhandled. However,  A variation of the pattern can be made to send the request to all the handlers by removing the condition from the handler and always calling the successor. e.g. moving the code to traverse the chain into the base class keeping the request handling in a different method in the subclasses.
  • As with the Observer pattern, Chain of Responsibility can make it difficult to follow through the logic of a particular path in the code at runtime.
  • If new operations need to be added to the Handler, modifying the source code is required.
  • Do not use Chain of Responsibility when each request is only handled by one handler, or, when the client object knows which service object should handle the request.

Sample Code


Pipeline Pattern
Pipeline or Filter pattern is similar to CoR pattern, however instead of each Concrete Handler has a reference  to the Successor in the chain, There is a handler manager that does this. The flexibility of the Pipeline pattern comes from the fact that at any time, a new Handler can be injected into the pipeline through the PipelineManager.


Sample Code


Related Patterns
  • Chain of Responsibility, Command, Mediator, and Observer, address how you can decouple senders and receivers, but with different trade-offs. Chain of Responsibility passes a sender request along a chain of potential receivers.
  • Chain of Responsibility can use Command to represent requests as objects.
  • Chain of Responsibility is often applied in conjunction with Composite. There, a component’s parent can act as its successor.

References
http://sourcemaking.com/design_patterns/chain_of_responsibility
http://www.oodesign.com/chain-of-responsibility-pattern.html
http://java.dzone.com/articles/design-patterns-uncovered-chain-of-responsibility
http://www.javacamp.org/designPattern/chains.html
http://doanduyhai.wordpress.com/2012/07/08/design-pattern-the-pipeline/

Futher readings
http://nchain.sourceforge.net/index.html
http://commons.apache.org/chain/
http://parlab.eecs.berkeley.edu/wiki/_media/patterns/pipeline-v1.pdf
http://blogs.clariusconsulting.net/kzu/pipelines-using-iterators-lambda-expressions-and-extension-methods-in-c-3-0/

Comments

  1. I am so happy to found your blog post because it's really very informative. Please keep writing this kind of blogs and I regularly visit this blog. Have a look at my services.
    Cyber Security Training Course in Chennai | Certification | Cyber Security Online Training Course | Ethical Hacking Training Course in Chennai | Certification | Ethical Hacking Online Training Course | CCNA Training Course in Chennai | Certification | CCNA Online Training Course | RPA Robotic Process Automation Training Course in Chennai | Certification | RPA Training Course Chennai | SEO Training in Chennai | Certification | SEO Online Training Course

    ReplyDelete
  2. Useful article which was very helpful. also interesting and contains good information.
    to know about python training course , use the below link.

    Python Training in chennai

    Python Course in chennai



























    ReplyDelete
  3. I have been looking for this article for a very long time. And finally, I found it. Best I have ever read about blog commenting.
    Thanks for sharing buddy.

    Java Training in Chennai

    Java Course in Chennai

    ReplyDelete
  4. Decent post. I learn something absolutely new and testing on sites I StumbleUpon consistently. It's consistently valuable to peruse articles from different writers and work on something from different destinations.
    live

    ReplyDelete
  5. Thanks for spending all your pleasant time to make such a Creative content for us. AWS Training in Chennai

    ReplyDelete
  6. Windows 10 Download is the most convenient activator to license, manage, activate, and organize your Microsoft Office and Windows. Microsoft Toolkit Download

    ReplyDelete
  7. Whenever you have to say farewell to someone don't ever say goodbye, say see you later or in till next time but never goodbye. Saying goodbye is like saying see Friendship GoodBye Quotes

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. mai dr. mahesh chand mai ek healthcare doctor hu ajj kal dekha jaye to female me health ki issue kafi dekhi jaati hai jaise body me josh nahi aana energy ki kami or lazy hona mahilao ko josh ki goli ka name agar aaplog me vi aisa problem aarha hai to meri site pe jakr sollustion dekhe or dawa ka name dekhe

    ReplyDelete
  10. Plus, they grant beneficiant 코인카지노 bonuses to players all around the} world. If you take pleasure in taking part in} a game of roulette, then you’re most likely conscious of how anticipation is a significant part of of} the sport. It’s a thrilling game that can keep you on your toes throughout the entire game.

    ReplyDelete

Post a Comment

Popular Posts