310 likes | 602 Views
SWE 316: Software Design and Architecture. Lecture # 15 Behavioral Design Patterns. Ch 9. To learn the behavioral design patterns and when to use them. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission. Interpreter. Iterator.
E N D
SWE 316: Software Design and Architecture Lecture # 15Behavioral Design Patterns Ch 9 • To learn the behavioral design patterns and when to use them. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Interpreter Iterator Mediator Observer State Chain of Responsibility 2/22 Patterns • Behavioral Design Patterns capture behavior among objects • Interpreter • Iterator • Mediator • Observer • State • Chain of Responsibility • Command • Template
Interpreter Iterator Mediator Observer State Chain of Responsibility 3/22 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 Iterator Mediator Observer State Chain of Responsibility 4/22 Interpreter Design Pattern KEY CONCEPT Design Goal At Work: Flexibility , Correctness, Reuse 1..n AbstractExpression interpret() Client -- a form for parsing and a means of processing expressions. TerminalExpression interpret() NonTerminalExpression interpret()
Interpreter Iterator Mediator Observer State Chain of Responsibility 5/22 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 Mediator Observer State Chain of Responsibility 6/22 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 Mediator Observer State Chain of Responsibility 7/22 Using Iterator Functions • /* • To perform desiredOperation() on elements of the aggregate according to the iteration (order) i: • */ • for( i.setToFirst(); !i.isDone(); i.increment() ) • desiredOperation( i.getCurrentElement() );
Interpreter Iterator Mediator Observer State Chain of Responsibility 8/22 Functions for Iterator // Iterator "points" to first element: voidsetToFirst(); //trueif iterator "points" past the last element: booleanisDone(); // Causes the iterator to point to its next element: void increment(); // Return the element pointed toby the iterator: C getCurrentElement();
Interpreter Iterator Mediator Observer State Chain of Responsibility 9/22 Iterator in Arrays, Vector, and in General
Interpreter Iterator Mediator Observer State Chain of Responsibility 10/22 Imagining Iterator Key: Intended sequence of Element objects element: Element After first() executes, iterator references this object. Aggregate of Element objects iterator: Iterator Before increment() executes, iterator references this object. After increment() executes, iterator references this object.
Interpreter Iterator Mediator Observer State Chain of Responsibility 11/22 Mediator • Design Purpose • Avoid references between dependent objects. • Design Pattern Summary • Capture mutual behavior in a separate class.
Interpreter Iterator Mediator Observer State Chain of Responsibility 12/22 Solicitation of Customer Information 1 of 2 Name and Location Basic information
Interpreter Iterator Mediator Observer State Chain of Responsibility 13/22 Solicitation of Customer Information 2 of 2 Account Information Customer ID Total business Amount due Additional information
Interpreter Iterator Mediator Observer State Chain of Responsibility 14/22 The Mediator Class Model Mediator Colleague ConcreteColleague1 ConcreteColleague2 ConcreteMediator
Interpreter Iterator Mediator Observer State Chain of Responsibility 15/22 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.
Interpreter Iterator Mediator Observer State Chain of Responsibility 16/22 Observer Design Pattern Server part Client part Client of this system 1 Observer update() Source notify() 1..n 2 for all Observer’s o: o.update();
Interpreter Iterator Mediator Observer State Chain of Responsibility 17/22 Observer Design Pattern Server part Client part Observer update() Source notify() Client 1 1..n 2 for all Observer’s o: o.update(); ConcreteObserver observerState update() ConcreteSource state 3
Interpreter Iterator Mediator Observer State Chain of Responsibility 18/22 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.
Interpreter Iterator Mediator Observer State Chain of Responsibility 19/22 State Design Pattern Structure doRequest() behaves according to state of Target Client { targetState.handleRequest(); } Target doRequest() TargetState handleRequest() targetState 1 TargetStateA handleRequest() TargetStateB handleRequest() . . . . . .
Interpreter Iterator Mediator Observer State Chain of Responsibility 20/22 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.
Interpreter Iterator Mediator Observer State Chain of Responsibility 21/22 Other Patterns • Command • Template • Will not be covered
Interpreter Iterator Mediator Observer State Chain of Responsibility 22/22 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 • Commandcaptures function flexibly (e.g. undo-able) • Templatecaptures basic algorithms, allowing variability