1 / 91

Chapter 9 Behavioral Design Patterns

Chapter 9 Behavioral Design Patterns. Process Phase Affected by This Chapter. Requirements Analysis. Design. Framework. Architecture. Detailed Design. Implementation. Key:. = main emphasis. = secondary emphasis. x. x.

fionn
Télécharger la présentation

Chapter 9 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. Chapter 9Behavioral Design Patterns

  2. Process Phase Affected by This Chapter Requirements Analysis Design Framework Architecture Detailed Design Implementation Key: = main emphasis = secondary emphasis x x Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  3. Interpreter 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. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  4. Interpreter Client Interface AbstractExpression interpret() Client Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  5. Interpreter Design Pattern 1..n AbstractExpression interpret() Client TerminalExpression interpret() NonTerminalExpression interpret() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  6. Interpreter Sequence Diagram :Client AbstractExpression :NonterminalExpression NonterminalExpression interpret() ... create & interpret() ... TerminalExpression ... create & interpret() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  7. Example of a Virtual Machine “Program” 400Mhz & 128MB assemble …. 260Mhz & 64MB 260Mhz & 32MB Graphics reproduced with permission from Corel. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  8. Input For Network Assembly Example Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  9. Output of Network Assembly Example (1 of 3) Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  10. Output of Network Assembly Example (2 of 3) Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  11. Output of Network Assembly Example (3 of 3) Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  12. Design Goal At Work: Flexibility , Correctness, Reuse Separate the processing of each part of the network order. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  13. Interpreter Design Pattern Client Component assemble() component1 NetSystem assemble() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  14. Application of Interpreter Design Pattern 0..1 Component assemble() Client 1 component1 NetSystem assemble() component2 cpu CPU describe() Setup getInputFromUser() parse() Computer assemble() RAM describe() ram Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  15. Key Concept: Interpreter Design Pattern  -- a form for parsing and a means of processing expressions. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  16. 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 Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  17. Iterator Design Purpose (Gamma et al) 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. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  18. 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() ); Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  19. Functions for Iterator // Iterator "points" to first element: void setToFirst(); //trueif iterator "points" past the last element: boolean isDone(); // Causes the iterator to point to its next element: void increment(); // Return the element pointed toby the iterator: C getCurrentElement(); Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  20. Iterator in Arrays, Vector, and in General Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  21. Imagining Iterator Key: Intended sequence of Element objects element: Element Aggregate of Element objects After first() executes, iterator references this object. iterator: Iterator Before increment() executes, iterator references this object. After increment() executes, iterator references this object. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  22. Iterator Example Setup Code // Suppose that we have iterators for forward and // backward order: we can re-use print_employees() List employees = new List(); ForwardListIterator fwd // to go from front to back = new ForwardListIterator ( employees ); ReverseListIterator bckwd // to go from back to front = new ReverseListIterator ( employees ); client.print_employees( fwd ); // print from front to back client.print_employees( bckwd ); // print from back to front Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  23. Iterator Class Model Client Iterator setToFirst() increment() isDone() getCurrentItem() Aggregate 1…n ConcreteIterator AggregatedElement ConcreteAggregate 1 Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  24. An Organizational Chart Example Vanna Presley vice president, 4 years Sue Miller svce. mgr., 7 years Sal Monahan svce. mgr., 1 year Sam Markham svce. mgr., 5 years Inez Clapp ind. contrib., 11 Inky Conway ind. contrib., 6 Iolanthe Carp ind. contrib., 8 Inge Carlson ind. contrib., 12 Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  25. Iterating by Organizational Seniority Over a Bank Organization Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  26. Iterating by Years of Service Over an Organization Chart Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  27. Design Goal At Work: Flexibility , Correctness Separate the “visiting” procedure from the processing of individual employees. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  28. Class Structure for Iterator Example Client Setup OrgChartIteratorDP OrgChartDP OrgChartIterator first() next() isDone() currentItem() Employee display() Teller ServiceIterator Supervisor OrgSeniorityIterator Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  29. Computing next() DeepRoot root doneNode ( 1 ) ( 0 ) 1 2 3 6 4 5 7 8 10 11 9 12 Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission. ( 1, 2, 0, 0 ) // my distance from the end of my sibling list )

  30. Sequence Diagram for Encounter Foreign Character Use Case User :Setup Employee main() AlphabeticalOrgChartIterator Get org chart as in Composite example tbd OrgChartIterator :Client getTheIterator() display( OrgCharIterator ) first(), next(), idDone(), getItem() etc. Employee display() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  31. Computing next() DeepRoot “distance from the end of my sibling list” root doneNode ( 1 ) ( 0 ) 1 2 3 ( 1, 2 ) 4 5 6 7 8 ( 1, 2, 0 ) 9 10 11 12 ( 1, 2, 0, 0 ) Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  32. Iterator Example Sequence Diagram User :Setup Employee AlphabeticalOrgChartIterator main() OrgChartIterator :Client Get org chart as in Composite example tbd getTheIterator() display( OrgCharIterator ) first(), next(), isDone(), getItem() etc. Employee display() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  33. Iterator in the Java API Iterator next() hasNext() remove() Collection Iterator iterator() ListIterator List AbstractCollection Iter* AbstractList ListIterator listIterator() ListItr* ArrayList Vector * IBM implementation Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  34. Output for Iteration on a Java ArrayList Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  35. Address Book Application Using Java Iterator Iterator next() hasNext() remove() Collection Iterator iterator() List ListIterator AbstractList ListIterator listIterator() ListItr* ArrayList Key: This application 0..n NameAddrEntry AddressBook Java API Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  36. Iterator Class Model Iterator setToFirst() increment() isDone() getCurrentItem() Aggregate createIterator( ) Client OLD ConcreteAggregate createIterator( ) ConcreteIterator return new ConcreteIterator( this ); Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  37. Output for StringTokenizer Example Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  38. Key Concept: Iterator Design Pattern  -- to access the elements of a collection. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  39. Solicitation of Customer Information 1 of 2 Name and Location Basic information Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  40. Solicitation of Customer Information 2 of 2 Account Information Customer ID Total business Amount due Additional information Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  41. Mediator Design Purpose Avoid references between dependent objects. Design Pattern Summary Capture mutual behavior in a separate class. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  42. The Mediator Class Model Mediator Colleague ConcreteColleague1 ConcreteColleague2 ConcreteMediator Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  43. 1. Initiation by ConcreteMediator :ConcreteColleague1 :ConcreteColleague2 :ConcreteMediator doPart1() Mediator Sequence Diagrams doPart2() 2. Initiation on a ConcreteColleague :Mediator mediate() mediate() doSome1() doSome2() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  44. Design Goal At Work: Reusability and Robustness Avoid hard-coded dependencies among the game’s GUI classes, enabling their use in other contexts. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  45. Application of Mediator To Customer Information Application CustomerGUI CustomerDisplay :BasicInfoGUI: :CustTypeDisplay :CustInfoDisplay Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  46. The Mediator Class Model in the Java API ComponentListener actionPerformed() Component addComponentListener() MyComponent1 MyComponent2 MyEventListener Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  47. Key Concept: Mediator Design Pattern  -- to capture mutual behavior without direct dependency. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  48. Observer Design Purpose Arrange for a set of objects to be affected by a single object. Design Pattern Summary The single object aggregates the set, calling a method with a fixed name on each member. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  49. 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(); Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  50. 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 Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

More Related