This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Chain of responsibilty Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Chain of responsibilty" Watch "Chain of responsibilty" New topic
Author

Chain of responsibilty

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

any example in java?


Groovy
HS Thomas
Ranch Hand

Joined: May 15, 2002
Posts: 3404
The event of filtering e-mail is an example of Chain of Responsibiity.
Therefore for instance, if the e-mail is spam e-mail it does not get forwarded to the recipients e-mail box.
Think of it as a Client not sending the request(e-mail) directly to the recipient but to many objects in between until one is found to handle it.
Eventually the right mail gets to the recipient's box or recipients' e-mail box in the case of group or bulk mail.
The aim of the Chain of Responsibility pattern is to decouple the sender and reciever of a request.So you shouldn't have code that sends a request to a particular reciever directly IMHO. We could try writing an example in Java and see how far we get.

Hope this helps.
regards
[ November 09, 2003: Message edited by: HS Thomas ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The AWT container hierarchy is an implementation of chain of responsibility. When an event is posted to a button or other GUI component, it either handles the event or posts it to its container.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
HS Thomas
Ranch Hand

Joined: May 15, 2002
Posts: 3404
Another example:
When a user enters a date, a chain of parsers then try to decode the date.
regards.
[ November 11, 2003: Message edited by: HS Thomas ]
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Servlet Filters (since 2.3 I guess) and I think even Apache Axis handlers follow the pattern.
HS Thomas
Ranch Hand

Joined: May 15, 2002
Posts: 3404

Now this code can be refactored to use Chain of Responsibility. Where do I start ? I'll re-visit this in a little while to flesh it out a little. I'm trying to get Poseidon to work.
regards
[ November 11, 2003: Message edited by: HS Thomas ]
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Is there any requirement that a single object has to handle the reuest or many objects in the chain can handle processing so that each object can produce some output of value to the client.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
The original chain of responsibility pattern states that only one member in the chain handles the "event". If the event is handled by 0..n members, it's no longer a chain of responsibility -- no one is responsible anymore, but everyone has an option to do something with the event.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
Don't get me started about those stupid light bulbs.
 
subject: Chain of responsibilty