Chapter 9 Behavioral Design Patterns Requirements Analysis Process
Chapter 9 Behavioral Design Patterns
Requirements Analysis Process Phase Affected by This Chapter Design Framework. Architecture. Detailed Design Key: x = main emphasis x = secondary emphasis Implementation Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
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.
Interpreter Client Interface Client Abstract. Expression interpret() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Interpreter Design Pattern Client Abstract. Expression 1. . n interpret() Terminal. Expression. Non. Terminal. Expression interpret() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Interpreter Sequence Diagram : Client Abstract. Expression : Nonterminal. Expression interpret() . . create & interpret() Terminal. Expression create & interpret() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Example of a Virtual Machine “Program” assemble …. 400 Mhz & 128 MB 260 Mhz & 64 MB 260 Mhz & 32 MB Graphics reproduced with permission from Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Input For Network Assembly Example Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Output of Network Assembly Example (1 of 3) Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Output of Network Assembly Example (2 of 3) Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Output of Network Assembly Example (3 of 3) Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Design Goal At Work: Flexibility , Correctness, Re 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.
Client Interpreter Design Pattern Component assemble() Net. System assemble() componen t 1 Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Component Client assemble() 0. . 1 Application of Interpreter Design Pattern 1 component 1 Net. System assemble() cpu Computer assemble() CPU RAM ram describe() component 2 Setup get. Input. From. User() parse() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
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.
Purpose of Iterator q - given a collection of objects e. g. , o the videos in a video store o a directory q Aggregate object - having specified ways to progress through them e. g. , q o “list in alphabetical order” iterator 2 o “list all videos currently on loan” iterator 7 . . . encapsulate each of these ways Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
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.
Using Iterator Functions /* To perform desired. Operation() on elements of the aggregate according to the iteration (order) i: */ for( i. set. To. First(); !i. is. Done(); i. increment() ) desired. Operation( i. get. Current. Element() ); Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Functions for Iterator // Iterator "points" to first element: void set. To. First(); // true if iterator "points" past the last element: boolean is. Done(); // Causes the iterator to point to its next element: void increment(); // Return the element pointed to by the iterator: C get. Current. Element(); Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Iterator in Arrays, Vector, and in General Index (integer) i on array my. Array The Iterato r Index (integer) j on Vector my. Vector Iterator (object) my. Iterator Operations Set to beginning i=0 j=0 Increment Get current element Check not done yet ++i my. Array[ i ] i< my. Array. length ++j my. Vector. get( j ) j< my. Vector. size() my. Iterator !. set. To. First. increment. get. Curren my. Iterator () () t Element(). is. Done() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Imagining Iterator element: Element iterator: Iterator After first() executes, iterator references this object. After increment() executes, iterator references this object. Key: Intended sequence of Element objects Aggregate of Element objects Before increment() executes, iterator references this object. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Iterator Example Setup Code // Suppose that we have iterators forward and // backward order: we can re-use print_employees() List employees = new List(); Forward. List. Iterator fwd // to go from front to back = new Forward. List. Iterator ( employees ); Reverse. List. Iterator bckwd // to go from back to front = new Reverse. List. Iterator ( employees ); client. print_employees( fwd ); back client. print_employees( bckwd ); front // print from front to // print from back to Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Iterator Class Model Client Iterator set. To. First() increment() is. Done() get. Current. Item() Concrete. Iterator Aggregate 1…n Aggregated. Element 1 Concrete. Aggregat Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
An Organizational Chart Example Vanna Presley vice president, 4 years Sue Miller svce. mgr. , 7 years Sam Markham svce. mgr. , 5 years Sal Monahan svce. mgr. , 1 year Inez Clapp Inky Conway Iolanthe Carp Inge Carlson ind. contrib. , 11 ind. contrib. , 6 ind. contrib. , 8 ind. contrib. , 12 Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Iterating by Organizational Seniority Over a Bank Organization Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
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.
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.
Class Structure for Iterator Example Client Setup Org. Chart. Iterator. DP Org. Chart. Iterator first() next() is. Done() current. Item() Employee display() Teller Service. Iterator Supervisor Org. Seniority. Iterator Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Computing next() root (1) 1 4 5 done. Node 2 6 9 Deep. Root (0) 3 7 8 10 11 12 ( 1, 2, 0, 0 ) // my distance from the end of my sibling list ) Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
User : Setup Employee Sequence Diagram for Encounter Foreign Character Use Case main() Alphabetical. Org. Chart. Iterator Get org chart as in Composite example tbd Org. Chart. Iterator : Client get. The. Iterator() display( Org. Char. Iterator ) first(), next(), id. Done(), get. Item() etc. Employee display() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Computing next() Deep. Root “distance from the end of my sibling list” ( 1, 2 ) 4 (1) 2 1 5 6 9 done. Node root 3 7 ( 1, 2, 0 ) 10 (0) 11 8 12 ( 1, 2, 0, 0 ) Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Iterator Example Sequence Diagram User : Setup Employee Alphabetical. Org. Chart. Iterator main() Get org chart as in Composite example tbd get. The. Iterator() Org. Chart. Iterator : Client display( Org. Char. Iterator ) first(), next(), is. Done(), get. Item() etc. Employee display() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Iterator in the Java API next() Collection has. Next() Iterator iterator() remove() List. Iterator List Abstract. Collection Iter* Abstract. List. Iterator list. Iterator() List. Itr* Array. List * IBM implementation Vector Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Output for Iteration on a Java Array. List Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Address Book Application Using Java Iterator next() has. Next() remove() Collection Iterator iterator() List. Iterator List. Itr* Abstract. List. Iterator list. Iterator() Array. List Key: This application Java API Name. Addr. Entry 0. . n Address. Book Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Iterator Class Model Aggregate Client create. Iterator( ) OLD Concrete. Aggregate Iterator set. To. First() increment() is. Done() get. Current. Item() Concrete. Iterator create. Iterator( ) return new Concrete. Iterator( this ); Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Output for String. Tokenizer Example Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
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.
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.
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.
Mediator Design Purpose void references between dependent object 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.
The Mediator Class Model Mediator Colleague Concrete. Colleague 1 Concrete. Colleague 2 Concrete. Mediator Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
1. Initiation by Concrete. Mediator : Concrete. Colleague 1 : Concrete. Colleague 2 : Concrete. Mediator do. Part 1() do. Part 2() 2. Initiation on a Concrete. Colleague mediate() Mediato r Sequen ce Diagra ms : Mediator mediate() do. Some 1() do. Some 2() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Design Goal At Work: Reusability and Robustnes 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.
Application of Mediator To Customer Information Application Customer. GUI : Basic. Info. GUI: Customer. Display : Cust. Type. Display : Cust. Info. Display Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
The Mediator Class Model in the Java API Component. Listener Component action. Performed() add. Component. Listener() My. Component 1 My. Component 2 My. Event. Listener Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
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.
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.
Observer Design Pattern Server part 1 Source notify() Client part Client of this system 1. . n Observer update() 2 for all Observer’s o: o. update(); Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Observer Design Pattern Server part Client 1 Client part Observer 1. . n update() Source notify() 2 for all Observer’s o: o. update(); Concrete. Observer observer. State update() Concrete. Source state 3 Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Server part Observer Applied to International Hamburger Co. Client part Client Source notify() 1. . n Observer update() Senior. Management forecast Headquarters Marketing update() demand marketing. Demand update() if( abs( hq. demand - marketing. Demand ) >do. Display() 0. 01 ) { marketing. Demand = hq. get. Demand(); do. Display(); } Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
I/O Example for Mutual Fund Observer Example Note: Hi. Growth. Mutual. Fund starts with 3 shares of Awesome, assumes price of 1. 0, and has non-Awesome holdings totalling 400. 0 Note: Med. Growth. Mutual. Fund starts with 2 shares of Awesome, assumes price of 1. 0, and has non-Awesome holdings totalling 300. 0 Note: Lo. Growth. Mutual. Fund starts with 1 shares of Awesome, assumes price of 1. 0, and has non-Awesome holdings totalling 200. 0 Enter 'quit': Any other input to continue. go on Enter the current price of Awesome Inc. in decimal form. 32. 1 Value of Lo Growth Mutual Fund changed from 201. 0 to 232. 1 Value of Med Growth Mutual Fund changed from 302. 0 to 364. 2 Value of Hi Growth Mutual Fund changed from 403. 0 to 496. 3 Enter 'quit': Any other input to continue. go on Enter the current price of Awesome Inc. in decimal form. 21. 0 Value of Lo Growth Mutual Fund changed from 232. 1 to 221. 0 Value of Med Growth Mutual Fund changed from 364. 2 to 342. 0 Value of Hi Growth Mutual Fund changed from 496. 3 to 463. 0 Enter 'quit': Any other input to continue. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Design Goal At Work: Flexibility Allow mutual funds objects to easily acquire or divest of stocks. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Observable notify. Observers() Mutual. Fund value num. Awesome. Shares Setup Client Observer update( Observable, Object ) Awesome. Inc price Java API Class Key: Developer Class Observer Example: Mutual Funds Long. Term. Mutual. Fund …. Medium. Term. Mutual. Fund update(…) …. Hi. Growth. Mutual. Fund update(…) …. update(…) Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Observer in the Java API Observable notify. Observers() My. Observable Observer update( Observable, Object ) My. Concrete. Observer observer. State subject update(…) Key: Java API Class Developer Class Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Key Concept: Observer Design Pattern -- to keep a set of objects up to date with the state of a designated object. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
State 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. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
State Design Pattern Structure: do. Request() behaves according to state of Target Client { target. State. handle. Request(); } Target do. Request() target. State Target. State. A handle. Request() Target. State 1 handle. Request() Target. State. B handle. Request() . . . Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
GUI For a Role-Playing Video Game Set Characteristics Adapted Corel from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission. Courtesy Tom Van. Court and
Design Goal At Work: Correctness and Reusabil Separate the generic code for handling button clicks from the actions which depend on the game’s status at the time. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
State Design Pattern Applied to Role-Playing Game My. Game. State set. Characteristics() { } handle. Click() state. handle. Click(); Setting. Up Waiting Setting. Characteristics. Engaging handle. Click() { } show. Window(); …. // more { } handle. Click() { // already responding … // display message show. Window(); } …. // more handle. Click() { // do nothing … // display message } Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Key Concept: State Design Pattern -- to cause a object’s functions to behave according to the state it’s in. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Chain of Responsibility Design Purpose Allow a set of objects to service a request. Present clients with a simple interface. Design Pattern Summary Link the objects in a chain via aggregation, allowing each to perform some of the responsibility, Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Chain of Responsibility: Object Model entry. Handler: Handler. Subclass. X (next element): Handler. Subclass. Z successor (next element): Handler. Subclass. Y Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Chain of Responsibility Class Model Client Handler handle. Request() Handler. Subclass. A handle. Request() respond. A() successor Handler. Subclass. B handle. Request(). . respond. B() respond. A(); // handle some of the required functionality successor. handle. Request(); // pass along remaining responsibility Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
GUI For Customer Information Application Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Output for Customer Information Application Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Design Goal At Work: Flexibility Isolate the responsibilities of each part of the input form to generate its XML. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Text. Field. Listener «client» Each of these classes supports handle. Click() Class Model For User Information Collection Customer. Info. Element container Customer. Personal Customer. Address. Company. Customer. Professional Customer. Name Customer. Info. Company. Name Customer. Info. App «setup» Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Object Model Fragment for Customer Information Example : Company. Name container : Company container handle. Click() : Customer. Professional container handle. Click() : Customer. Info handle. Click() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Key Concept: Chain of Responsibility Design Pat -- to distribute functional responsibility among a collection of objects. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Command Design Purpose Increase flexibility in calling for a service e. g. , allow undo-able operations. Design Pattern Summary Capture operations as classes. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
The Command Design Pattern Client Command replaced execute() Target 1 Action 1 Command action 1() execute() Target 2 Action 2 Command action 2() execute() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
The Command Design Pattern: Example command Menu. Item Command handle. Click() execute() document. cut() command. execute() Cut. Command document execute() Document document. copy() cut() copy() Copy. Command document execute() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Sequence Diagram for Document Command Example : Menu. Item handle. Click() command : Copy. Command : Command document execute() : Document copy() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
I/O for Word Processor Undo Example 1 of 2 Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
I/O for Word Processor Undo Example 2 of 2 Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Design Goal At Work: Flexibility and Robustness Isolate the responsibilities of the Word Processor commands, making them save -able and reversible. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Word. Processor Command command. History 0. . n execute() undo() document Document text: String Document. Command document get. Input. From. User() Undo. Command Quit. Command. Cut. Command. Paste. Command execute() Undo Example Class Model text. Cut: String offset: int execute() undo() offset: int length: int execute() undo() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Key Concept: Command Design Pattern -- to avoid calling a method directly (e. g. , so as to record or intercept it). Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Example of Template Motivation • Required to solve equations of the form ax 2 + bx + c = 0. • Must be able to handle all input possibilities for a, b, and c. • This is a tutorial application that must provide full explanations to users about Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
A Basic Quadratic Algorithm 1. Report progress 2. Display number of solutions 3. Display first solution, if any 4. Display second solution, if any Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Template Design Purpose Allow runtime variants on an algorithm. Design Pattern Summary Express the basic algorithm in a base class, using method calls where variation is required. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Client Class Model for Template. Algorithm algorithm Subject do. Request() { algorithm. handle. Request(); } handle. Request() non. Inherited. Method() called. Method 1() called. Method 2() …. Algorithm 1 Algorithm 2 Algorithm 3 called. Method 1() called. Method 2() …. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
I/O For Quadratic Example 1/2 Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
I/O For Quadratic Example 2/2 Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
display. Solutions() Quadratic Display Algorithm { display. Whats. Happening(); display. Num. Solutions(); display. First. Solutions(); display. Second. Solution(); Override } Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Design Goal At Work: Flexibility and Robustness Isolate the main algorithm for quadratic solution display. Isolate the variants that depend on the coefficients. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Client Setup Class Model for Quadratic Problem get. ABC() Quadratic. Algorithm Quadratic. Equation algorithm float a, b, c solve. Quadratic() display. Whats. Happening() display. Solution() input. Makes. Sense() display. Num. Solutions(); display. First. Solution() display. Second. Solution() { algorithm. display. Solution(); } No. Solutions One. Solution Two. Solutions Infinitely. Many. Solutions display. Num. Solutions() display. First. Solution() display. Second. Solution() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Key Concept: Template Design Pattern -- to capture a basic algorithm and its variants. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Behavioral Design Patterns capture behavior among objects Summary of o Interpreter handles expressions in grammers Behavioral o Iterator visits members of a collection Patterns o Mediator captures behavior among peer objects o Observer updates objects affected by a single object o State allows method behavior to depend on current status o Chain of Responsibility allows a set of objects to provide functionality collectively Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
- Slides: 91