chapter 6 introduction to design patterns. sample design goals and ways to accomplish them...
TRANSCRIPT
![Page 1: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/1.jpg)
Chapter 6
Introduction to Design Patterns
![Page 2: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/2.jpg)
Sample Design Goals and Ways to Accomplish Them
Reusability, Flexibility, and Efficiencyo Reuse flexible designso Keep code at a general levelo Minimize dependency on other classes
Robustness o Reuse reliable designso Reuse robust parts
Sufficiency / Correctnesso Modularize designo Reuse trusted parts
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 3: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/3.jpg)
Key Concept: Design Pattern
A design pattern is a combination of classes and accompanying algorithms that fulfill a common design purpose.
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 4: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/4.jpg)
6.1 - Categories of Design Patterns
![Page 5: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/5.jpg)
Key Concept: Creational Design Patterns
-- used to create objects in flexible or constrained ways.
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Factory Abstract Factory Prototype Singleton
![Page 6: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/6.jpg)
Key Concept: Structural Design Patterns
-- used to represent data structures such as linked lists or trees, with uniform processing interfaces.
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Composite Decorator Adapter Facade Flyweight Proxy
![Page 7: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/7.jpg)
Key Concept: Behavioral Design Patterns
-- used to capture behavior among objects.
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Chain of Responsibility Command Interpreter Mediator Observer State Template Iterator
![Page 8: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/8.jpg)
6.2 - Example Use of a Creational Design Pattern
Abstract Factory
![Page 9: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/9.jpg)
KitchenViewer Interface
Wallcabinet
Counter
Floorcabinet
Modern Classic Antique Arts & Crafts
menu
display area
styles
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 10: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/10.jpg)
KitchenViewer Example
Modern Classic Antique Arts & Crafts
Wall cabinets Floor cabinetsCountertop
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 11: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/11.jpg)
Selecting Antique Style
Modern Classic Antique Arts & CraftsAdapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 12: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/12.jpg)
KitchenViewer Without Design Patterns
Kitchen
ClientrenderKitchen()
FloorCabinet
ModernWallCabinet
ModernFloorCabinet AntiqueFloorCabinet
AntiqueWallCabinet
WallCabinet
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 13: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/13.jpg)
Design Goal At Work: Flexibility
Our design should be flexible enough to produce any of several kitchen styles.
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 14: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/14.jpg)
AntiqueKStylegetWallCabinet()getFloorCabinet()
The Abstract Factory Idea
KitchenStylegetWallCabinet()getFloorCabinet()
ModernKStylegetWallCabinet()getFloorCabinet()
WallCabinet FloorCabinet
AntiqueWallCabinet AntiqueFloorCabinet
FloorCabinet getFloorCabinet() { return new AntiqueFloorCabinet(); }
……
FloorCabinet getFloorCabinet() { return new ModernFloorCabinet(); }
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
A design class
![Page 15: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/15.jpg)
Abstract Factory Design Pattern Applied to KitchenViewer
KitchenStylegetWallCabinet()getFloorCabinet()
KitchengetWallCabinet()getFloorcabinet()
ClientrenderKitchen( KitchenStyle )
ModernKStylegetWallCabinet()getFloorCabinet()
AntiqueKStylegetWallCabinet()getFloorCabinet()
WallCabinet FloorCabinet
ModernWallCabinet
ModernFloorCabinet
AntiqueWallCabinet
AntiqueFloorCabinetAdapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 16: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/16.jpg)
Example of Code inside renderKitchen(KitchenStyle myStyle)
// Create the wall cabinets; type is determined by the class of myStyleWallCabinet wallCabinet1 = myStyle.getWallCabinet();WallCabinet wallCabinet2 = myStyle.getWallCabinet();
// Create the floor cabinets; type determined by the class of myStyleFloorCabinet floorCabinet1 = myStyle.getFloorCabinet();FloorCabinet floorCabinet2 = myStyle.getFloorCabinet();
// Create the kitchen object (in the style required)Kitchen kitchen = new Kitchen();kitchen.add(wallCabinet1, . . .);kitchen.add(wallCabinet2, . . .);. . .kitchen.add(floorCabinet1, . . .);kitchen.add(floorCabinet2, . . .);. . .
![Page 17: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/17.jpg)
Abstract Factory Design Pattern
StylegetComponentA()getComponentB()
ClientdoOperation( Style myStyle )
Style1getComponentA()getComponentB()
Style2getComponentA()getComponentB()
ComponentA ComponentB
Style1ComponentA
Style1ComponentB
Style2ComponentA
Style2ComponentB
Collection
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 18: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/18.jpg)
Abstract Factory Design PatternAlternative
StylegetComponentA()getComponentB()
ClientdoOperation()
Style1getComponentA()getComponentB()
Style2getComponentA()getComponentB()
ComponentA ComponentB
Style1ComponentA
Style1ComponentB
Style2ComponentA
Style2ComponentB
Collection getComponentA()getComponentB()
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 19: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/19.jpg)
6.3 - Example Use of a Behavioral Design Pattern
Mediator
![Page 20: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/20.jpg)
Example of Behavioral Design Goal: Port Traffic
berthberth
to drydock
obstacles
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 21: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/21.jpg)
Ship Tugboat1..n1
Harbor application
A customs application: reuses Ship alone
Ship Longshoreman
Avoiding Dependencies
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 22: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/22.jpg)
Mediator Concept Applied to The Harbor Problem
Ship
Tugboat
LeavingPortestimateTime()
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 23: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/23.jpg)
Applying the Mediator Design Pattern to The Harbor Problem
Ship Tugboat
VesselPortMissionestimateTime()
LeavingPortestimateTime()
EnteringPortestimateTime()
BeingMaintainedestimateTime()
Mediator base class
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 24: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/24.jpg)
6.4 - Characteristics of Design Patterns
![Page 25: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/25.jpg)
Characteristics of Design Patterns 1
Viewpoints – ways to describe patterns1. Static: class model (building blocks)
2. Dynamic: sequence or state diagram (operation)
Levels – decomposition of patterns1. Abstract level describes the core of the pattern
2. Concrete (= non abstract) level describes the particulars of this case
Roles – the “players” in pattern usage 1. Application of the design pattern itself
2. Clients of the design pattern application
3. Setup code initializes and controls
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 26: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/26.jpg)
Key Concept: Two Viewpoints
We consider design patterns from the static viewpoint (what they are made from) and the dynamic viewpoint (how they function).
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 27: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/27.jpg)
Characteristics of Design Patterns 21. Client role
2. Setup role
A
C
A. Static viewpoint B. Dynamic viewpoint
3. Role: Application of the design pattern
D
B (i) Abstract level
(ii) Concrete level
(class model)(sequence or
state diagram)
(class or classes)
(class or classes)
: Reference directionAdapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 28: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/28.jpg)
Key Concept: Two Levels
Design patterns usually have an abstract level and a non-abstract (“concrete”) level.
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 29: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/29.jpg)
Concrete and Abstract Layers
KitchenStyle
Kitchen
Client
ModernKStyle
AntiqueKStyle
WallCabinet
FloorCabinet
ModernWallCabinet
ModernFloorCabinet
AntiqueWallCabinet
AntiqueFloorCabinet
Abstract level
Concrete level
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 30: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/30.jpg)
Design Goal At Work: Correctness
We want to provide an interface to a design pattern so that its functionality is clear and separate.
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 31: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/31.jpg)
Key Concept: Three Roles
Each part of a design pattern fills one of three roles:1) Applies (i.e., instantiates) the design pattern, or2) is a client of the pattern application, or3) reinitializes or sets up the two previous roles
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 32: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/32.jpg)
Design Goal At Work: Correctness
To use design patterns effectively, we distinguish the roles involved.
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 33: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/33.jpg)
2. Client Role 1. Design Pattern Application
Interface to the pattern
(frequently abstract; possibly
several classes)
DPClient DPInterface
Rest of thedesign pattern
application
Interacts with the design pattern only through its interface
3. Setup Role
The Three Roles Involved in Design Pattern Usage
Rest of the Application
No limitations
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.DP = Design Pattern
![Page 34: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/34.jpg)
6.5 - Forms of Design Patterns
![Page 35: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/35.jpg)
Design Pattern Forms
-- Forms represent patterns to the design patterns (i.e., metapatterns)-- They consist of delegation forms and recursion forms
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 36: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/36.jpg)
Basic Idea of Delegation
requiredFunction()
… intermediaryFunction( … ){ … requiredFunction() …}
ClientclientFunction() intermediaryFunction()
replace
… clientFunction( … ){… intermediaryFunction( … ) …}
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 37: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/37.jpg)
Basic Design Pattern Form #1: Delegation
DoerBasedoIt()
DPInterfaceinterfaceMethod()
ConcreteDoer1doIt()
ConcreteDoer2doIt()
. . .
doerObject
… interfaceMethod( … ){ … doerObject.doIt() … }
Client
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 38: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/38.jpg)
Basic Design Pattern Form #2: Recursion
RecursionBasedoOperation()
NonrecursiveClassdoOperation()
RecursiveClassdoOperation()
void doOperation( … ){ … aggregate … }
aggregate
Client
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 39: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/39.jpg)
The Recursion Form Applied to an Organization Chart
EmployeeprintOrganization()
IndividualContributorprintOrganization()
SupervisorprintOrganization()
void printOrganization( … ){ … supervisees.printOrganization() … }
supervisees
Client
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 40: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/40.jpg)
Key Concept: Two Forms
A design pattern’s form is usually either a delegation of responsibility or a class that relates to itself (recursion).
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 41: Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs](https://reader035.vdocuments.site/reader035/viewer/2022062511/55189b23550346881f8b462c/html5/thumbnails/41.jpg)
Summary of This Chapter
Design Patterns are recurring designs satisfying
recurring design purposes
Classified as Creational, Structural, or Behavioral
Described by Static and Dynamic Viewpoints o Typically class models and sequence diagrams respectively
Use of a pattern application is a Client Roleo Client interface carefully controlled
o “Setup,” typically initialization, a separate role
Design patterns Forms are Delegation or Recursion
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.