Presentation is loading. Please wait.

Presentation is loading. Please wait.

Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.

Similar presentations


Presentation on theme: "Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD."— Presentation transcript:

1 Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD

2 Behavioral Patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor

3 Chain of Responsibility Allows multiple classes to handle a request without having to coordinate. Heavily used in Java, particularly in exception handling. The chain might be dynamically altered. Approach: –Set up a chain of responsible classes or objects and walk down the chain until a class claims the request. –Often implemented as a tree. –The chain has to be implemented using interfaces. Positives: –Decouples handlers –Flexible Negatives: –No coordination –Falling off the end of the chain or tree

4 Command Encapsulate a request as an object. Supports queueing, logging, and undo operations. The Java 1.1 event model implements this. Approach: –Encapsulate the request as an object. Positives: –Hides the private details of the request. –Commands are first class objects. –Allows priority queueing. –Supports a macro or scripting language approach. Negatives: –Overhead –Lots of little classes

5 Interpreter Some problems are best defined in terms of statements in a language. The interpreter pattern provides a grammar and a way of translating statements. Examples include scripting languages. Java provides facilities for parsing XML and regular expressions. Positives: –The grammar is easy to change and extend. –Interpreting simple languages is easy. Negatives: –Efficiency –Only simple languages can be easily processed.

6 Iterator An iterator is an object that supports the traversal of a collection. The interface between an algorithm and a collection should be an iterator. Approach: –See the Iterator interface. Consider implementing by using inner classes. Positives: –Raises the level of abstraction without a major performance penalty. Negatives: –There are some esoteric traps to avoid if the collection is simultaneously being modified. –Protected access –Typecasting is necessary in Java

7 Mediator In a complex system, supporting intercommunication between classes may become tangled. A mediator is a switchboard. Approach: –Forward messages to the appropriate objects via the mediator object. The mediator usually handles initialization as well. Positives: –Loose coupling between objects –The mediator encapsulates the business relationships. Negatives: –Potentially monolithic in complexity, producing a maintenance burden. –Overhead –Lack of reuse

8 Memento Can capture and externalize the state of objects to allow them to be examined or restored later. Used for undo/redo and also for database transaction logging. Approach: –Use a second class in the same package to capture and restore the internal state. Positives: –Needed for state machines. –Needed for white-box testing and debugging. Negatives: –Object state can be complex and hard to capture. –Potentially large amounts of data.

9 Observer Supports separation between model and view. Approach: see Swing Positives: –Decouples observers from models Negatives: –Lack of coordination between observers. Who has responsibility? –What sort of messages should be sent? Adapter classes may be required.

10 State An object that has to appear to change its class suggests the use of the state pattern Approach: –Encapsulate the variable behavior in a state attribute. To switch states, change the object stored in the attribute. Keep prototypical states stored in the state class. Positives: –State machines are a good design solution –Avoids large numbers of conditional statements Negatives: –Unless the prototype pattern is used, there can be a lot of object creation. –Lack of function pointers in Java means that there is significant overhead. –Tables often work better.

11 Strategy Sometimes you need to encapsulate a collection of algorithms in a class hierarchy. The algorithms are allowed to vary independently of the classes using them. The choice of algorithm reflects factors unknown until program execution. Approach: –The strategy class objects are functors—their behavior is defined by a static member function with a common name, but with behavior varying among the subclasses. Positives: –Elegant –Effective Negatives: –The client code has to be able to choose a strategy. –The signature of the functor may have to be quite broad.

12 Template Method Define the skeleton of an algorithm, but defer details to subclasses. Fundamental to Java. Approach: –Abstract classes provide this capability. Positives: –Normal part of OO programming. Negatives: –Not as powerful as in C++.

13 Visitor Sometimes you need to perform a common operation on a number of class objects in multiple classes. This can be useful in white-box testing. Approach: –Move the behavior to an outside class: the visitor, which traverses the collection of objects. The objects need to provide an appropriate interface. The operation is done by the visitor accessing the object interface, which in return calls the polymorphic operation provided by the visitor. Positives: –Minimizes code duplication –Makes adding new operations easy Negatives: –Arcane and inefficient. –Adding new classes affected by the visitor is hard. –The objects to be visited need to be kept in a collection.

14 Examples from Book

15 Pattern Importance More  Less Decorator State Iterator Façade Strategy Proxy Factory Method Adaptor Observer Template Method Composite Singleton Abstract Factory MVC Bridge Builder Chain of Responsibility Flyweight Interpreter Mediator Memento Prototype Visitor


Download ppt "Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD."

Similar presentations


Ads by Google