1 / 22

Lecture # 15 Behavioral Design Patterns

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.

lacey
Download Presentation

Lecture # 15 Behavioral Design Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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

  3. 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.

  4. 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()

  5. 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

  6. 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.

  7. 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() );

  8. 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();

  9. Interpreter Iterator Mediator Observer State Chain of Responsibility 9/22 Iterator in Arrays, Vector, and in General

  10. 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.

  11. 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.

  12. Interpreter Iterator Mediator Observer State Chain of Responsibility 12/22 Solicitation of Customer Information 1 of 2 Name and Location Basic information

  13. 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

  14. Interpreter Iterator Mediator Observer State Chain of Responsibility 14/22 The Mediator Class Model Mediator Colleague ConcreteColleague1 ConcreteColleague2 ConcreteMediator

  15. 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.

  16. 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();

  17. 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

  18. 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.

  19. 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() . . . . . .

  20. 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.

  21. Interpreter Iterator Mediator Observer State Chain of Responsibility 21/22 Other Patterns • Command • Template • Will not be covered 

  22. 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

More Related