none
Chain of Responsibility pattern RRS feed

  • Question

  • I have been looking into design pattern especially the Chain of Responsibility pattern(from Design Patterns Explained Simply ebook). The explanation states that " 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.".

    But some of the examples online (eg:http://www.coderslexicon.com/chain-of-responsibility-pattern-c/) does not  delegates the request to base class, but passes the request to the next in the list. Is it truly a requirement to pass a request to base class if not handled by the current object so that the base class then route the request to next?

    Sunday, February 21, 2016 4:44 PM

Answers

  • Hello,

    hmmm interesting :) I think yes in this example, in my opinion it depends how they can pass pointer to next, but if it is child which inherit from parent, it must call parent's methode, maybe only overlay with it's implementation. If I good understand, in that first formula must that next handler call base class method to pass pionter to next one via base class pointer. In second system is use diferent system, I think perfect idea (directly can't be handler be created) so f.ex.

    Handler *h1 = new specialHandler(10, 1);

    is legal - very interesting, it's abstract class so only via this can be created child. But in my opinion there is also call of parent methode

    virtual void request(int value) = 0;

    next->request(value);

    I think that this is call of base class. Very interesting game, maybe I'll try also something to do, pls give info how you'll live.

    Edit: maybe this

    h1->setNextHandler(h2);

    is manual setup of that chaining, what you've asked...

    Regards...

    Duris_I





    • Edited by Duris_I Sunday, February 21, 2016 6:39 PM
    • Proposed as answer by Duris_I Monday, February 22, 2016 1:14 PM
    • Marked as answer by May Wang - MSFT Friday, March 4, 2016 6:35 AM
    Sunday, February 21, 2016 6:07 PM

All replies

  • Hello,

    hmmm interesting :) I think yes in this example, in my opinion it depends how they can pass pointer to next, but if it is child which inherit from parent, it must call parent's methode, maybe only overlay with it's implementation. If I good understand, in that first formula must that next handler call base class method to pass pionter to next one via base class pointer. In second system is use diferent system, I think perfect idea (directly can't be handler be created) so f.ex.

    Handler *h1 = new specialHandler(10, 1);

    is legal - very interesting, it's abstract class so only via this can be created child. But in my opinion there is also call of parent methode

    virtual void request(int value) = 0;

    next->request(value);

    I think that this is call of base class. Very interesting game, maybe I'll try also something to do, pls give info how you'll live.

    Edit: maybe this

    h1->setNextHandler(h2);

    is manual setup of that chaining, what you've asked...

    Regards...

    Duris_I





    • Edited by Duris_I Sunday, February 21, 2016 6:39 PM
    • Proposed as answer by Duris_I Monday, February 22, 2016 1:14 PM
    • Marked as answer by May Wang - MSFT Friday, March 4, 2016 6:35 AM
    Sunday, February 21, 2016 6:07 PM
  • Thank you Duris_I. I was curious about it really possible to leave out "delegation to the base class."
    • Edited by its_me_here Monday, February 22, 2016 12:03 PM
    Monday, February 22, 2016 11:51 AM
  • Yes, thank you too to show some interesting topic. Maybe other experts say complete oposite opinions that help us understand this better.

    Regards...

    Duris_I

    Monday, February 22, 2016 1:13 PM
  • From the design patterns gang of four, quoted.

    Implementing the successor chain.There are two possible ways to implement the successor chain: 
    a. Define new links (usually in the Handler, but ConcreteHandlers could define them instead).

    b. Use existing links. 

    For (a), the example shows:

    class HelpHandler {public: HelpHandler(HelpHandler* s) : _successor(s) { } virtual void HandleHelp(); private: HelpHandler* _successor; }; void HelpHandler::HandleHelp () { if (_successor) { _successor->HandleHelp(); }  }

    It is necessary to delegate to the base class to form a chain though the easiest way is calling HelpHandler::HandleHelp () if ConcreteHandler::HandleHelp () is NOT successful. The idea of using the base class in my opinion is based on the Template Method pattern to set up the workflow in the base which is nice but again not necessary for chain of responsibility pattern. 

    HTH,

    Patrick.



    Patrick

    Monday, February 22, 2016 4:12 PM