




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





1
Chain of Responsibility Pattern Apr. 10, 2007
3
can handle it.•^ The request is passed along until one of the classesbetween them.the only common link is the request that is passed•^ It provides a loose coupling between these classes;capabilities of the other classes.without any of them knowing about thenumber of classes to attempt to handle a request,•^ The Chain of Responsibility pattern allows a
control forwards its ID or name to the chain.•^ When you select an area for help, that visualgeneric help is the only suitable result.there are window background areas where moreapplication invites you to seek help, but in whichsystem, where every screen region of an•^ One example of such a chain pattern is a Help
5
a response in all cases.•^ There is no guarantee that the request will producegeneral.•^ The chain is organized from most specific to most
7
13
15
could implement any number.•^ This example only interprets 3 colors, but youdisplays it if it can.interprets the message as a color name and•^ In a similar fashion, the ColorImage class simply
17
forward the message to other classes as well.•^ However, to allow for convenient extension, we are able tocommand that reaches it is simply displayed in the list.•^ The RestList class is the end of the chain, and anyadapter here.lecture to give JList a simpler interface, we’ll use that•^ Since we developed an adapter JawtList in a previousare JList boxes.•^ Both the file list and the list of unrecognized commands
19
replicating thederived from the RestList class, to avoid•^ The FileList class is quite similar and can be
(^) addChain (^) and getChain (^) methods.
receives a request.initialized, and looks for one of those files when itfiles in the current directory into the list when•^ The only differences are that it loads a list of the
25
traversed.at a time until the entire system had beenat the bottom, and going left to right and up a rowcould align the classes into a single chain, starting•^ There is little reason for that complexity -- youmay preclude the need for the chain at all.•^ This can complicate the design in some cases, andthe chain.each button, or its handler, knows where to enter•^ However, this sort of structure seems to imply that
27
derived classes with additional methods.designing an abstract Request type and any number ofproperties of such a request object, you might end up•^ Since various classes along the chain may use differentcomplete object with a number of methods.•^ The information could include various data types or aexample.than just the string that we conveniently used on thisResponsibility may well be a great deal more complicated•^ The request or message passed along the Chain of
into play.implementations of that method does not come•^ The fact that further parents contain othercontaining that method is found.inheritance chain until the first parent classderived class, that method is passed up the•^ If you call for a method to be executed in a deeplyitself.Responsibility is the class inheritance structure•^ The most obvious example of the Chain of