Back to list
Views:   24.8K
Replies:  4

Chain of Responsibility vs Observer pattern

What is the difference between the Chain of Responsibility pattern and Observer pattern?
And what cases would you use what?
sam grimmer, Aug 21, 2010
Reply 1
Well, do not think there are any similarities. Observer is used when set of different objects work together and maintain a state (properties) while they work in tandom. They tend to provide a set of services or continue performing a job . When another object that has defined their existing state changes, all of them get notified by a third object and all observers change their state (properties) to now provide services as per new state.
Chain of responsibility would be when a job needs to be handled by a single object out of many objects and it is unkown as to who would handle this. instead of factory object creating a specific instance, a default object handles the request, checks if he is the one who should handle this, if not passes on to another one .. the entire chain has a sequence and is not arbitrarily checks.
Nunu Kohli, Dec 25, 2013
Reply 2
Thanks alot for your answers. It is now alot clearer.
sam grimmer, Aug 22, 2010
Reply 3

I will try to give my best understanding. Views from others are welcome.

Scenario 1: Observer Pattern

Let us assume you work in a team of 5 people and all of you report to a director of IT. Your director holds a team meeting every monday morning at 10 AM. Your director has a assistant who take care of his administrative tasks. On one monday morning, your director does not come to the meeting. You guys then go to the assistant and ask her to inform you guys when the director arrives, so that you guys can reassemble for the meeting. In 5 minutes, you get some emergency call from your house and you inform the assistant and leave for the day.

There are few important items to be noted here.

a. team member including you - observers
b. assistant - subject
c. director's arrival - event

As per the above scenario, there is a sequence of activities to be understood.

Team members (observers) ask (register or attach) the assistant (subject) to inform if the director arrives (event). So the assistant (subject) carefully maintains a list of all team members who have asked (registered) her to inform about the director's arrival (event). Also when you (observer) tell her that you are leaving for the day, she (subject) removes you (observer) from the list. When the director arrives (event), the assistant (subject) sends an email (notifies) to all the team members (observers) in the list.

Scenario 2: Chain of Responsibility

Let us assume you spend some money for offical purpose. Then you submit your bill in a reimbursement system in your office. The workflow is as below.

a. project leader - can approve if the amount is less than $100
b. project manager - can approve if the amount is less than $200
c. vice president - should approve if the amount is greater than $200

So the chain of responsibility looks as below.

project leader --> project manager --> vice president

So in terms of the approval process, project leader is first level. Project manager is successor or next level for project leader. Vice president is successor or next level for project manager. So your claim will flow in the respective chain of responsibility and get processed.

Sreenivas Manyam Rajaram, Aug 21, 2010
Reply 4

This is my take on this. Both Chain of Responsibility and Observer address how you can decouple the senders and the receivers, but does so slightly different.

Observer is about defining a one-to-many dependency between objects. Meaning that when one object changes state, all its subscribers are notified and updated automatically.

Chain of Responsibility uses a kind of pipeline where each object gets its chance to handle a request. The base class keeps track of which handler to send the request to (one at a time), and if the request needs to be "passed on", then the handler "calls back" to the base class, which delegates to the next handler in the pipeline.

Chain of Responsibility pattern is not recommended to be used when each request is only handled by one handler, or, when the client object knows which service object should handle the request.

Robert Blixt, Aug 21, 2010
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.