Presentation is loading. Please wait.

Presentation is loading. Please wait.

SWE 316: Software Design and Architecture Objectives Lecture # 15 Behavioral Design Patterns SWE 316: Software Design and Architecture  To learn the behavioral.

Similar presentations


Presentation on theme: "SWE 316: Software Design and Architecture Objectives Lecture # 15 Behavioral Design Patterns SWE 316: Software Design and Architecture  To learn the behavioral."— Presentation transcript:

1 SWE 316: Software Design and Architecture Objectives Lecture # 15 Behavioral Design Patterns SWE 316: Software Design and Architecture  To learn the behavioral design patterns and when to use them. Ch 9 Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

2 SWE 316: Software Design and Architecture Patterns  Behavioral Design Patterns capture behavior among objects  Interpreter  Iterator  Mediator  Observer  State  Chain of Responsibility  Command  Template InterpreterIteratorMediatorObserverStateChain of Responsibility 2/22

3 SWE 316: Software Design and Architecture Interpreter Design Pattern  Design Purpose  Interpret expressions written in a formal grammar.  Design Pattern Summary  Represent the grammar using a Recursive design pattern form: Pass interpretation to aggregated objects. Interpreter IteratorMediatorObserverStateChain of Responsibility 3/22

4 SWE 316: Software Design and Architecture AbstractExpression interpret() Client TerminalExpression interpret() NonTerminalExpression interpret() 1..n Interpreter Design Pattern -- a form for parsing and a means of processing expressions. KEY CONCEPT Design Goal At Work: Flexibility, Correctness, Reuse Interpreter IteratorMediatorObserverStateChain of Responsibility 4/22

5 SWE 316: Software Design and Architecture Purpose of Iterator  given a collection of objects e.g.,  the videos in a video store  a directory  having specified ways to progress through them e.g.,  “list in alphabetical order”  “list all videos currently on loan” ... encapsulate each of these ways Aggregate object iterator2 iterator7 Interpreter Iterator MediatorObserverStateChain of Responsibility 5/22

6 SWE 316: Software Design and Architecture Iterator Design Pattern  Design Purpose  Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.  Design Pattern Summary  Encapsulate the iteration in a class pointing (in effect) to an element of the aggregate. Interpreter Iterator MediatorObserverStateChain of Responsibility 6/22

7 SWE 316: Software Design and Architecture /* To perform desiredOperation() on elements of the aggregate according to the iteration (order) i : */ for( i.setToFirst(); !i.isDone(); i.increment() ) desiredOperation( i.getCurrentElement() ); Using Iterator Functions Interpreter Iterator MediatorObserverStateChain of Responsibility 7/22

8 SWE 316: Software Design and Architecture // Iterator "points" to first element: void setToFirst(); // true if iterator "points" past the last element: boolean isDone(); // Causes the iterator to point to its next element: void increment(); // Return the element pointed to by the iterator: C getCurrentElement(); Functions for Iterator Interpreter Iterator MediatorObserverStateChain of Responsibility 8/22

9 SWE 316: Software Design and Architecture Iterator in Arrays, Vector, and in General Iterator Operations Set to beginning Increment Get current element Check not done yet The Iterator Index (integer) i on array myArray i = 0++imyArray[ i ] i < myArray.length Index (integer) j on Vector myVector j = 0++j myVector.get( j ) j < myVector.size() Iterator (object) myIterator.setToFirst() myIterator.increment() myIterator.getCurrent Element() ! myIterator.isDone() Interpreter Iterator MediatorObserverStateChain of Responsibility 9/22

10 SWE 316: Software Design and Architecture iterator: Iterator element: Element Aggregate of Element objects Key: Intended sequence of Element objects After first() executes, iterator references this object. After increment() executes, iterator references this object. Before increment() executes, iterator references this object. Imagining Iterator Interpreter Iterator MediatorObserverStateChain of Responsibility 10/22

11 SWE 316: Software Design and Architecture Mediator  Design Purpose  Avoid references between dependent objects.  Design Pattern Summary  Capture mutual behavior in a separate class. InterpreterIterator Mediator ObserverStateChain of Responsibility 11/22

12 SWE 316: Software Design and Architecture Solicitation of Customer Information 1 of 2 Name and Location Basic information InterpreterIterator Mediator ObserverStateChain of Responsibility 12/22

13 SWE 316: Software Design and Architecture Solicitation of Customer Information 2 of 2 Account Information Additional information Customer ID Total business Amount due InterpreterIterator Mediator ObserverStateChain of Responsibility 13/22

14 SWE 316: Software Design and Architecture MediatorColleague ConcreteMediator ConcreteColleague1ConcreteColleague2 The Mediator Class Model InterpreterIterator Mediator ObserverStateChain of Responsibility 14/22

15 SWE 316: Software Design and Architecture Observer Design Pattern  Design Purpose  Arrange for a set of objects to be affected by a single object.  to keep a set of objects up to date with the state of a designated object  Design Pattern Summary  The single object aggregates the set, calling a method with a fixed name on each member. InterpreterIteratorMediator Observer StateChain of Responsibility 15/22

16 SWE 316: Software Design and Architecture Source notify() Observer update() for all Observer’s o: o.update(); Client of this system 1 2 1..n Server partClient part Observer Design Pattern InterpreterIteratorMediator Observer StateChain of Responsibility 16/22

17 SWE 316: Software Design and Architecture Source notify() Observer update() ConcreteSource state ConcreteObserver observerState update() for all Observer’s o: o.update(); Client 2 3 1..n Server partClient part 1 Observer Design Pattern InterpreterIteratorMediator Observer StateChain of Responsibility 17/22

18 SWE 316: Software Design and Architecture State Design Pattern  Design Purpose  Cause an object to behave in a manner determined by its state.  Design Pattern Summary  Aggregate a State object and delegate behavior to it. InterpreterIteratorMediatorObserver State Chain of Responsibility 18/22

19 SWE 316: Software Design and Architecture TargetStateB handleRequest() Target doRequest() doRequest() behaves according to state of Target targetState TargetState handleRequest() Client TargetStateA handleRequest() 1 { targetState.handleRequest(); }... State Design Pattern Structure InterpreterIteratorMediatorObserver State Chain of Responsibility 19/22

20 SWE 316: Software Design and Architecture Chain of Responsibility Design Pattern  Design Purpose  Allow a set of objects to service a request. Present clients with a simple interface.  to distribute functional responsibility among a collection of objects.  Design Pattern Summary  Link the objects in a chain via aggregation, allowing each to perform some of the responsibility, passing the request along. InterpreterIteratorMediatorObserverState Chain of Responsibility 20/22

21 SWE 316: Software Design and Architecture Other Patterns  Command  Template  Will not be covered InterpreterIteratorMediatorObserverStateChain of Responsibility 21/22

22 SWE 316: Software Design and Architecture Summary of Behavioral Patterns Behavioral Design Patterns capture behavior among objects  Interpreter handles expressions in grammers  Iterator visits members of a collection  Mediator captures behavior among peer objects  Observer updates objects affected by a single object  State allows method behavior to depend on current status  Chain of Responsibility allows a set of objects to provide functionality collectively  Command captures function flexibly (e.g. undo-able)  Template captures basic algorithms, allowing variability InterpreterIteratorMediatorObserverStateChain of Responsibility 22/22


Download ppt "SWE 316: Software Design and Architecture Objectives Lecture # 15 Behavioral Design Patterns SWE 316: Software Design and Architecture  To learn the behavioral."

Similar presentations


Ads by Google