Docsity

Lecture Slides on Behavior Patterns - Programming Language Technology | CMSC 433, Study notes of Programming Languages

Toggle sidebar
bg1
1
Behavioral Patterns
• Patterns that are most specifically concerned with communication between
objects.
– The Iterator pattern formalizes the way we move through a list of data within a
class.
– The Observer pattern defines the way a number of classes can be notified of a
change.
– The Mediator defines how communication between classes can be simplified by
using another class to keep all classes from having to know about each other.
– The Chain of Responsibility allows an even further decoupling between classes, by
passing a request between classes until it is recognized.
– The Template pattern provides an abstract definition of an algorithm.
– The Interpreter provides a definition of how to include language elements in a
program.
– The Strategy pattern encapsulates an algorithm inside a class.
– The Visitor pattern adds function to a class.
– The State pattern provides a memory for a class’s instance variables.
– The Command pattern provides a simple way to separate execution of a command
from the interface environ ment that produced it.
2
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2007
Chain of Responsibility Pattern
Apr. 10, 2007
3
What is it?
• The Chain of Responsibility pattern allows a
number of classes to attempt to handle a request,
without any of them knowing about the
capabilities of the other classes.
• It provides a loose coupling between these classes;
the only common link is the request that is passed
between them.
• The request is passed along until one of the classes
can handle it.
4
A Classic Example
• One example of such a chain pattern is a Help
system, where every screen region of an
application invites you to seek help, but in which
there are window background areas where more
generic help is the only suitable result.
• When you select an area for help, that visual
control forwards its ID or name to the chain.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Lecture Slides on Behavior Patterns - Programming Language Technology | CMSC 433 and more Study notes Programming Languages in PDF only on Docsity!

1

Behavioral Patterns

  • objects.•^ Patterns that are most specifically concerned with communication between from the interface environment that produced it.–^ The Command pattern provides a simple way to separate execution of a command–^ The State pattern provides a memory for a class’s instance variables.–^ The Visitor pattern adds function to a class.–^ The Strategy pattern encapsulates an algorithm inside a class.program.–^ The Interpreter provides a definition of how to include language elements in a–^ The Template pattern provides an abstract definition of an algorithm.passing a request between classes until it is recognized.–^ The Chain of Responsibility allows an even further decoupling between classes, byusing another class to keep all classes from having to know about each other.–^ The Mediator defines how communication between classes can be simplified bychange.–^ The Observer pattern defines the way a number of classes can be notified of aclass.The Iterator pattern formalizes the way we move through a list of data within a

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2007

Chain of Responsibility Pattern Apr. 10, 2007

3

What is it?

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

A Classic Example

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 Classic Example

  • •^ Suppose you selected the “New” button.
  • (^) If that doesn’t exist, the message is lost and no information is displayed.help module that tells you how the system works in general.– (^) If there is no general button help, the message is forwarded to the generaldisplay a general message about how buttons work.– (^) Eventually, the message is forwarded to an “All buttons” class that can– (^) If not, it forwards the request to the next module.message.If the first module can handle the New button, it displays the help

Note that…

a response in all cases.•^ There is no guarantee that the request will producegeneral.•^ The chain is organized from most specific to most

7

of objects that can handle requests.•^ You want to be able to dynamically modify the setobjects without specifying which one explicitly.•^ You want to issue a request to one of severalautomatically by the chain.to use. The handler must be determinedrequest and there is no way to know which handler•^ You have more than one handler that can handle aUse the Chain of Responsibility when…

A Simple Example

  • •^ These requests can berequests.•^ Consider a simple system for display the results of typed in
  • (^) Other commands– (^) Colors– (^) General filenamesImage filenames request text itself.request, and in the last case, we can only display the•^ In three cases, we can display a concrete result of the

13

Code of Imager Class

Code of Imager Class (cont…)

15

ColorImage Class

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

Code of ColorImage Class

17

The Other Two Classes

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

RestList Code

19

FileList

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

FileList Code

25

Disadvantage

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

The “Tree” Chain

27

More Complex Requests

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

Example in Java

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

Summary

  • between objects.•^ This approach also gives you added flexibility in distributing responsibilitiesrequest to other objects.coupling between objects. An object only needs to know how to forward the•^ The main purpose for this pattern, like a number of others, is to reduce chain and the responsibilities at run time.Any object can satisfy some or all of the requests, and you can change both the linking, sending and forwarding code in each module separately.•^ This disadvantage of this approach is that you often have to implement thethem all from JPanel.objects can inherit from another useful hierarchy, as we did here by derivingneeds to be an interface rather than an abstract class, so that the individual•^ Finally, since Java can not provide multiple inheritance, the basic Chain classhandle.however, the last object in the chain may simply discard any requests it can’t•^ An advantage is that there may not be any object that can handle the request,