chapter 7: s tructuraland ehavioral design patterns · 2012-11-14 · iterator design pattern the...

29
Software Engineering Design: Theory and Practice by Carlos E. Otero CHAPTER 7: STRUCTURAL AND BEHAVIORAL DESIGN P ATTERNS SESSION II: BEHAVIORAL DESIGN P ATTERNS, ITERATOR, OBSERVER Slides copyright © 2012 by Carlos E. Otero For non-profit educational use only May be reproduced only for student use when used in conjunction with Software Engineering Design: Theory and Practice. Any other reproduction or use is prohibited without the express written permission of the author. All copyright information must appear if these slides are posted on a website for student use. 10/24/2012 1 Software Engineering Design: Theory and Practice

Upload: others

Post on 15-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

Software Engineering Design: Theory and Practiceby Carlos E. Otero

CHAPTER 7: STRUCTURALAND BEHAVIORAL

DESIGN PATTERNS

SESSION II: BEHAVIORAL DESIGN PATTERNS,

ITERATOR, OBSERVER

Slides copyright © 2012 by Carlos E. Otero

For non-profit educational use only

May be reproduced only for student use when used in conjunction with Software Engineering Design:

Theory and Practice. Any other reproduction or use is prohibited without the express written

permission of the author.

All copyright information must appear if these slides are posted on a website for student use.

10/24/2012 1Software Engineering Design: Theory and Practice

Page 2: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

SESSION’SAGENDA

� Behavioral Patterns in Detailed Design

� Iterator

� Example…

� Observer…

� Example…

� What’s next…

10/24/2012 Software Engineering Design: Theory and Practice 2

Page 3: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

BEHAVIORALDESIGN PATTERNS

� Behavioral design patterns are patterns that deal with encapsulating behavior with objects, assigning responsibility, and managing object cooperation when achieving common tasks [1].

� They include many of the mainstream design patterns used in modern object-oriented frameworks and play a key role in the design of systems by making them independent of specific behavior, which is made replaceable with objects throughout these design patterns.

� Popular behavioral design patterns include:

� Iterator

� Observer

� Others not covered:

� Command

� Chain of responsibility

� Interpreter

� Mediator

� Memento

10/24/2012 Software Engineering Design: Theory and Practice 3

State

Strategy

Template Method

Visitor

Page 4: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

ITERATOR DESIGN PATTERN

� The Iterator design pattern is an object behavioral pattern that provides a

standardized way for accessing and traversing objects in a collection data

structure.

� According to the GoF, the intent of the Iterators to [1],

� Provide a way to access the elements of an aggregate object sequentially

without exposing its underlying representation.without exposing its underlying representation.

10/24/2012 Software Engineering Design: Theory and Practice 4

Iterator interface specifies a Iterator interface specifies a Iterator interface specifies a Iterator interface specifies a uniform way for traversing a uniform way for traversing a uniform way for traversing a uniform way for traversing a collection data structurecollection data structurecollection data structurecollection data structure

Concrete Iterators must obey Concrete Iterators must obey Concrete Iterators must obey Concrete Iterators must obey the Iterator interface!the Iterator interface!the Iterator interface!the Iterator interface!

Page 5: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

ITERATOR DESIGN PATTERN EXAMPLE

10/24/2012 Software Engineering Design: Theory and Practice 5

Page 6: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

ITERATOR DESIGN PATTERN EXAMPLE

10/24/2012 Software Engineering Design: Theory and Practice 6

Consider this case, where code in two Consider this case, where code in two Consider this case, where code in two Consider this case, where code in two different computer store classes use different computer store classes use different computer store classes use different computer store classes use different collection data structures, different collection data structures, different collection data structures, different collection data structures, each with its own unique interface!each with its own unique interface!each with its own unique interface!each with its own unique interface!

These could’ve been These could’ve been These could’ve been These could’ve been other collection data other collection data other collection data other collection data structures, arrays, etc.structures, arrays, etc.structures, arrays, etc.structures, arrays, etc.

Page 7: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

ITERATOR DESIGN PATTERN EXAMPLE

Different interfaces Different interfaces Different interfaces Different interfaces require different code!require different code!require different code!require different code!

If a new store is added, If a new store is added, If a new store is added, If a new store is added, we have to modify this we have to modify this we have to modify this we have to modify this code to add code for code to add code for code to add code for code to add code for displaying products of displaying products of displaying products of displaying products of

10/24/2012 Software Engineering Design: Theory and Practice 7

displaying products of displaying products of displaying products of displaying products of the new store!the new store!the new store!the new store!

Page 8: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

ITERATOR DESIGN PATTERN EXAMPLE

Notice how in this Notice how in this Notice how in this Notice how in this example we separate example we separate example we separate example we separate the collection data the collection data the collection data the collection data structures from the structures from the structures from the structures from the computer stores!computer stores!computer stores!computer stores!

10/24/2012 Software Engineering Design: Theory and Practice 8

Notice the pattern! Applying Notice the pattern! Applying Notice the pattern! Applying Notice the pattern! Applying the pattern to the computer the pattern to the computer the pattern to the computer the pattern to the computer store problem entails doing store problem entails doing store problem entails doing store problem entails doing

more of the same!more of the same!more of the same!more of the same!

Let’s see how this is implemented…Let’s see how this is implemented…Let’s see how this is implemented…Let’s see how this is implemented…

Concrete Iterators must obey the Concrete Iterators must obey the Concrete Iterators must obey the Concrete Iterators must obey the StoreProductIteratorStoreProductIteratorStoreProductIteratorStoreProductIterator interface to interface to interface to interface to provide a uniform way for traversing a collection data structure provide a uniform way for traversing a collection data structure provide a uniform way for traversing a collection data structure provide a uniform way for traversing a collection data structure

containing Computer Productscontaining Computer Productscontaining Computer Productscontaining Computer Products

Page 9: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

ITERATOR DESIGN PATTERN EXAMPLE

Common interface for Common interface for Common interface for Common interface for iterating through the iterating through the iterating through the iterating through the Notice that we’re Notice that we’re Notice that we’re Notice that we’re

using different using different using different using different

10/24/2012 Software Engineering Design: Theory and Practice 9

iterating through the iterating through the iterating through the iterating through the products!products!products!products!

Notice that we’re Notice that we’re Notice that we’re Notice that we’re using different using different using different using different

interface names than interface names than interface names than interface names than the original pattern!the original pattern!the original pattern!the original pattern!

Page 10: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

ITERATOR DESIGN PATTERN EXAMPLE

10/24/2012 Software Engineering Design: Theory and Practice 10

For this problem, we’ve For this problem, we’ve For this problem, we’ve For this problem, we’ve added some helper added some helper added some helper added some helper methods. It is OK to methods. It is OK to methods. It is OK to methods. It is OK to

modify the pattern based modify the pattern based modify the pattern based modify the pattern based on domain knowledge to on domain knowledge to on domain knowledge to on domain knowledge to fit the design to the fit the design to the fit the design to the fit the design to the

application!application!application!application!

Page 11: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

ITERATOR DESIGN PATTERN EXAMPLE

The Iterator used for The Iterator used for The Iterator used for The Iterator used for the simple storethe simple storethe simple storethe simple store

Requires a simple Requires a simple Requires a simple Requires a simple product data collection!product data collection!product data collection!product data collection!

10/24/2012 Software Engineering Design: Theory and Practice 11

Page 12: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

ITERATOR DESIGN PATTERN EXAMPLE

Details / differences are Details / differences are Details / differences are Details / differences are

Common interface allow Common interface allow Common interface allow Common interface allow clients to traverse through the clients to traverse through the clients to traverse through the clients to traverse through the _products objects in this _products objects in this _products objects in this _products objects in this

10/24/2012 Software Engineering Design: Theory and Practice 12

Details / differences are Details / differences are Details / differences are Details / differences are hidden from clients ! hidden from clients ! hidden from clients ! hidden from clients !

_products objects in this _products objects in this _products objects in this _products objects in this collection, regardless of the collection, regardless of the collection, regardless of the collection, regardless of the difference that may exist in difference that may exist in difference that may exist in difference that may exist in the _products interfaces!the _products interfaces!the _products interfaces!the _products interfaces!

Page 13: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

ITERATOR DESIGN PATTERN EXAMPLE

Details / differences are Details / differences are Details / differences are Details / differences are

Common interface allow Common interface allow Common interface allow Common interface allow clients to traverse through the clients to traverse through the clients to traverse through the clients to traverse through the _products objects in this _products objects in this _products objects in this _products objects in this

10/24/2012 Software Engineering Design: Theory and Practice 13

Details / differences are Details / differences are Details / differences are Details / differences are hidden from clients ! hidden from clients ! hidden from clients ! hidden from clients !

_products objects in this _products objects in this _products objects in this _products objects in this collection, regardless of the collection, regardless of the collection, regardless of the collection, regardless of the difference that may exist in difference that may exist in difference that may exist in difference that may exist in the _products interfaces!the _products interfaces!the _products interfaces!the _products interfaces!

Page 14: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

ITERATOR DESIGN PATTERN EXAMPLE

10/24/2012 Software Engineering Design: Theory and Practice 14

Page 15: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

ITERATOR DESIGN PATTERN EXAMPLE

We can now display product We can now display product We can now display product We can now display product information the same way for any information the same way for any information the same way for any information the same way for any store! With this design, if new store! With this design, if new store! With this design, if new store! With this design, if new stores are added, we can reuse the stores are added, we can reuse the stores are added, we can reuse the stores are added, we can reuse the same displayProduct method to same displayProduct method to same displayProduct method to same displayProduct method to

display products!display products!display products!display products!

10/24/2012 Software Engineering Design: Theory and Practice 15

Iterators are used all over the Java framework, C#, Iterators are used all over the Java framework, C#, Iterators are used all over the Java framework, C#, Iterators are used all over the Java framework, C#, and C++ standard template library!and C++ standard template library!and C++ standard template library!and C++ standard template library!

Page 16: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

THE JAVA ITERATOR AND LISTITERATOR INTERFACE

Java Java Java Java IteratorIteratorIteratorIterator

Java Java Java Java ListIteratorListIteratorListIteratorListIterator

10/24/2012 Software Engineering Design: Theory and Practice 16

Page 17: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

JAVA ENUMERATION INTERFACE

10/24/2012 Software Engineering Design: Theory and Practice 17

Page 18: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

ITERATOR DESIGN PATTERN

� The step-by-step approach for designing the Iterator design pattern is presented as follows:1. Identify and design the Iterator interface.

2. For each class representing a collection data structure in the software system, design a concrete Iterator and associate it with it. Implement the concrete iterator’smethods in terms of the collection data structure.

3. Create the Aggregate interface, which includes the interface method to create Iterators.

4. For each class representing a collection data structure, implement the Aggregate 4. For each class representing a collection data structure, implement the Aggregate interface to instantiate and return a concrete Iterator.

� Benefits of the Iterator design pattern include:� The Iterator provides a consistent way for clients to iterate through the objects in a

collection.

� It abstracts the internals of the collection objects so that if they change, clients do not have to change.

� Allows client code to be extended easily; numerous iterators can be created to support different traversals from the same or different collection structure.

10/24/2012 Software Engineering Design: Theory and Practice 18

Page 19: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

OBSERVER DESIGN PATTERN

� The Observer design pattern is an object behavioral pattern that standardizes the operations between objects that interoperate using a one-to-many relationship.

� According to the GoF, the intent of the Observer is to [1]

� Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

10/24/2012 Software Engineering Design: Theory and Practice 19

Page 20: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

OBSERVER DESIGN PATTERN EXAMPLE

10/24/2012 Software Engineering Design: Theory and Practice 20

Page 21: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

OBSERVER DESIGN PATTERN EXAMPLE

10/24/2012 Software Engineering Design: Theory and Practice 21

Page 22: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

OBSERVER DESIGN PATTERN EXAMPLE

10/24/2012 Software Engineering Design: Theory and Practice 22

Page 23: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

OBSERVER DESIGN PATTERN EXAMPLE

Register observers with the Register observers with the Register observers with the Register observers with the subject, in this case, the subject, in this case, the subject, in this case, the subject, in this case, the

DecisionEngineDecisionEngineDecisionEngineDecisionEngine

10/24/2012 Software Engineering Design: Theory and Practice 23

When a decision is made, the notify When a decision is made, the notify When a decision is made, the notify When a decision is made, the notify method is called to iterate through the method is called to iterate through the method is called to iterate through the method is called to iterate through the list of registered observers to update list of registered observers to update list of registered observers to update list of registered observers to update

them with the latest news!them with the latest news!them with the latest news!them with the latest news!

Page 24: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

JAVAOBSERVER INTERFACE

10/24/2012 Software Engineering Design: Theory and Practice 24

Page 25: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

JAVAOBSERVABLE

Observers are almost always used Observers are almost always used Observers are almost always used Observers are almost always used within the MVC architecture!within the MVC architecture!within the MVC architecture!within the MVC architecture!

10/24/2012 Software Engineering Design: Theory and Practice 25

Page 26: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

JAVAOBSERVABLEMETHODS

10/24/2012 Software Engineering Design: Theory and Practice 26

Page 27: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

OBSERVER DESIGN PATTERN

� The steps required to apply the Observer design pattern include:1. Design the Subject interface and implement code for attaching, detaching, and

notifying observer objects. The code for keeping track of observers can be done using existing linked-lists data structures.

2. For classes that manage information of interests to observers, inherit from the subject class created in step 1.

3. Design the Observer interface, which includes the abstract update interface method.

4. For all observers in the system, implement the Observer interface, which requires implementing the update method.implementing the update method.

5. At run-time, create each observer and attach them to the subject. When changes occur, the subject iterates through its list of registered objects, and calls their update method.

� Benefits of the Observer design pattern:� Flexibility for adding new services to the system.

� Since specific services are compartmentalized, maintain and modifying existing system services becomes easier.

10/24/2012 Software Engineering Design: Theory and Practice 27

Page 28: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

WHAT’S NEXT…

� In this session, we presented behavioral design patterns, including:

� Iterator

� Observer

� This concludes the presentation of design patterns in detailed design. In the

next module, we will present a different form of design which occurs

(mostly) at the function-level. We refer to this form of design as (mostly) at the function-level. We refer to this form of design as

construction design.

10/24/2012 Software Engineering Design: Theory and Practice 28

Page 29: CHAPTER 7: S TRUCTURALAND EHAVIORAL DESIGN PATTERNS · 2012-11-14 · ITERATOR DESIGN PATTERN The step-by-step approach for designing the Iterator design pattern is presented as follows:

REFERENCES

� [1] Gamma, Erich, Richard Helm, Ralph Johnson, and John Vlissides.

Design Patterns: Elements of Reusable Object-Oriented Software. Boston:

Addison-Wesley, 1995.

10/24/2012 Software Engineering Design: Theory and Practice 29