950 likes | 1.2k Views
Software Design Patterns. Main Reference: Software Engineering: Modern Approaches, 2 nd ed., 2010, Wiley. Chapter 17 Eric Braude and Michael Bernstein. Learning Goals. What are examples of a recurring design purposes? What are “creational” design patterns? What are “structural” ones?
E N D
Software Design Patterns Main Reference: Software Engineering: Modern Approaches, 2nd ed., 2010, Wiley. Chapter 17 Eric Braude and Michael Bernstein
Learning Goals • What are examples of a recurring design purposes? • What are “creational” design patterns? • What are “structural” ones? • What are “behavioral” design patterns? • Can design patterns be thought of in terms of roles and basic forms?
Design Pattern • Design Patterns are recurring designs satisfying recurring design purposes • Described by Static and Dynamic Viewpoints • Typically class models and sequence diagrams respectively • Use of a pattern application is a Client Role • Client interface carefully controlled • “Setup,” typically initialization, a separate role • Design patterns Forms usually Delegation or Recursion • Classified as Creational, Structural, or Behavioral
KitchenViewer Interface Wall cabinet menu Counter display area styles Floor cabinet Modern Classic Antique Arts & Crafts
KitchenViewer Example Wall cabinets Floor cabinets Countertop Modern Classic Antique Arts & Crafts
Selecting Antique Style Modern Classic Antique Arts & Crafts
KitchenViewer Without Design Patterns Client renderKitchen() Kitchen WallCabinet FloorCabinet ModernWallCabinet AntiqueWallCabinet ModernFloorCabinet AntiqueFloorCabinet
The Abstract Factory Idea KitchenStyle getWallCabinet() getFloorCabinet() WallCabinet FloorCabinet … AntiqueWallCabinet … AntiqueFloorCabinet ModernKStyle getWallCabinet() getFloorCabinet() AntiqueKStyle getWallCabinet() getFloorCabinet() FloorCabinet getFloorCabinet() { return new ModernFloorCabinet(); } FloorCabinet getFloorCabinet() { return new AntiqueFloorCabinet(); }
Abstract Factory Design Pattern Applied to KitchenViewer Client renderKitchen( KitchenStyle ) KitchenStyle getWallCabinet() getFloorCabinet() Kitchen getWallCabinet() getFloorcabinet() WallCabinet FloorCabinet ModernWallCabinet ModernKStyle getWallCabinet() getFloorCabinet() AntiqueWallCabinet AntiqueKStyle getWallCabinet() getFloorCabinet() ModernFloorCabinet AntiqueFloorCabinet
Abstract Factory Design Pattern Client doOperation( Style myStyle ) Style getComponentA() getComponentB() Collection ComponentA ComponentB Style1ComponentA Style1 getComponentA() getComponentB() Style2ComponentA Style2 getComponentA() getComponentB() Style1ComponentB Style2ComponentB
Abstract Factory Design PatternAlternative Client doOperation() Collection getComponentA() getComponentB() Style getComponentA() getComponentB() ComponentA ComponentB Style1ComponentA Style1 getComponentA() getComponentB() Style2ComponentA Style2 getComponentA() getComponentB() Style1ComponentB Style2ComponentB
Example of Behavioral Design Goal: Port Traffic obstacles Algorithms express mutual behavior to drydock berth berth
Avoiding Dependencies Harbor Application 1 1..n Ship Tugboat Customs application: reuse Ship alone Ship Longshoreman
Core Mediator Concept Applied to The Harbor Problem Ship LeavingPort estimateTime() Tugboat
Applying the Mediator Design Pattern to The Harbor Problem PortMission estimateTime() Vessel Mediator base class Ship Tugboat EnteringPort estimateTime() LeavingPort estimateTime() BeingMaintained estimateTime()
Characteristics of Design Patterns 1 • Viewpoints – ways to describe patterns • Static: class model (building blocks) • Dynamic: sequence or state diagram (operation) • Levels – decomposition of patterns • Abstract level describes the core of the pattern • Concrete (= non abstract) level describes the particulars of this case • Roles –the “players” in pattern usage • Application of the design pattern itself • Clients of the design pattern application • Setup code initializes and controls
(class or classes) Characteristics of Design Patterns 2 1. Client role 3. Role: Application of the design pattern 2. Setup role : Reference direction (class or classes)
(class or classes) Characteristics of Design Patterns 2 1. Client role 3. Role: Application of the design pattern A. Static viewpoint B. Dynamic viewpoint A B (i) Abstract level (ii) Concrete level C D (sequence or state diagram) (class model) 2. Setup role : Reference direction (class or classes)
Client myStyle:KitchenStyle Abstract Factory Application Sequence Diagram myStyle. getWallCabinet() -- IF myStyle BELONGS TO ModernKStyle -- myStyle: ModernKStyle wallCabinet1: ModernWallCabinet renderKitchen ( myStyle ) getWallCabinet() ModernWallCabinet()
Client myStyle:KitchenStyle Abstract Factory Application Sequence Diagram myStyle. getWallCabinet() -- IF myStyle BELONGS TO ModernKStyle -- myStyle: ModernKStyle wallCabinet1: ModernWallCabinet renderKitchen ( myStyle ) getWallCabinet() ModernWallCabinet() -- IF myStyle BELONGS TO AntiqueKStyle -- myStyle: AntiqueKStyle wallCabinet1: AntiqueWallCabinet getWallCabinet() AntiqueWallCabinet()
Concrete and Abstract Layers Client Abstract level Concrete level
Concrete and Abstract Layers Client WallCabinet KitchenStyle FloorCabinet Abstract level Concrete level Kitchen ModernWallCabinet AntiqueWallCabinet ModernKStyle ModernFloorCabinet AntiqueKStyle AntiqueFloorCabinet
The Three Roles Involved in Design Pattern Usage 2. Client Role 1. Design Pattern Application 3. Setup Role Rest of the Application
The Three Roles Involved in Design Pattern Usage 2. Client Role 1. Design Pattern Application Interacts with the design pattern only through its interface Interface to the pattern (frequently abstract; possibly several classes) DPInterface DPClient 3. Setup Role No limitations Rest of the design pattern application Rest of the Application
Singleton Design Purpose Ensure that there is exactly one instance of a class S. Be able to obtain the instace from anywhere in the application.
Singleton Design Purpose Ensure that there is exactly one instance of a class S. Be able to obtain the instace from anywhere in the application. Design Pattern Summary Make the constructor of S private; define a private static attribute for S of type S; define a public accessor for it.
Singleton: Class Model Client Singleton Design Pattern singletonOfMyClass MyClass getSingletonOfMyClass(): MyClass «static» 1
The Singleton Design Pattern -- applied to MyClass • Define a private static member variable of MyClass of type MyClass • private static MyClass singletonOfMyClass = new MyClass(); • Make the constructor of MyClass private • private MyClass() { /* …. constructor code …. */ }; • Define a public static method to access the member • public static MyClass getSingletonOfMyClass() • { • return singletonOfMyClass; • }
Application of Singleton to Experiment Example Experiment theExperiment: Experiment analyze() getTheExperiment(): Experiment reportResults() theExperiment Client «static» 1
Word Processor Interaction 1 of 2 ---> Enter title: My Life ---> Enter Heading or “-done”: Birth ---> Enter text: I was born in a small mountain hut …. ---> Enter Heading or “-done”: Youth ---> Enter text: I grew up playing in the woods … ---> Enter Heading or “-done”: Adulthood …. ---> Enter Heading or “-done”: -done (continued)
Word Processor Interaction 2 of 2:Output Options …. >> Enter the style you want displayed: big …. >> Enter the style you want displayed: small Option 1 Option 2 ----- Title: MY LIFE ----- Section 1. --- BIRTH --- I was born in a mountain hut …. Section 2. --- YOUTH --- I grew up sturdy … Section 3. --- ADULTHOOD --- …. My Life Birth I was born in a mountain hut …. Youth I grew up sturdy … Adulthood
Abstract Factory Design Purpose “Provide an interface for creating families of related or dependent objects without specifying their concrete classes.”* * Gamma et al
Abstract Factory Design Purpose “Provide an interface for creating families of related or dependent objects without specifying their concrete classes.”* Design Pattern Capture family creation in a class containting a factory method for each class in the family. * Gamma et al
Abstract Factory Interface Client Abstract Factory* AbstractFactory getAPart1Object() getAPart2Object() Ensemble setAbstractFactory() doAFunction() StyleAFactory StyleBFactory Style…. * relationships within pattern application not shown
Interface of Abstract Factory Applied to Word Processor Application of Abstract Factory 1 Document setStyle() display() Style SmallStyle LargeStyle . . . . . . . Client
The Abstract Factory Idea AbstractFactory getAPart1Object() getAPart2Object() abstractFactory 1 Ensemble setAbstractFactory() doAFunction() Part1 Part2 Part1StyleA Part2StyleA StyleAFactory getAPart1Object() getAPart2Object() «create» Client
Abstract Factory AbstractFactory getAPart1Object() getAPart2Object() abstractFactory 1 Ensemble doAFunction() 1..n 1..n Part1 Part2 Part… Part1StyleA Part1StyleB Part2StyleA Part2StyleB «create» StyleAFactory StyleBFactory Style…. Client
Sequence Diagram for Abstract Factory :Client abstractFactory :StyleXFactory :Ensemble } abstractFactory :AbstractFactory StyleXFactory() Selecting a style setAbstractFactory (abstractFactory ) :Part_iStyleX } doAFunction() getAPart_i() Creating a Part_i object in required style Assume that this method requires a Part_i object. getAPart_i() Part_iStyleX() Virtual function property
Document getAHeading() getATitle() grtDocumentFromUser() Style getAHeading() getATitle() Abstract Factory Applied to Word Processor style Displayable display() 0..n 0..n Heading value Text value 1 Title value Displayable SmallHeading display() LargeHeading display() SmallTitle display() LargeTitle display() «create» SmallStyle getAHeading() getATitle() LargeStyle getAHeading() getATitle() Client getStyleFromUser() displayDocument()
Facade Design Purpose Provide an interface to a package of classes Design Pattern Summary Define a singleton which is the sole means for obtaining functionality from the package. Notes: the classes need not be organized as a package; more than one class may be used for the façade.
Facade Design Pattern Structure 1 Façade «exposed» cMethodOfFacade() Client 2 C «not exposed» myCMethod()
Facade Design Pattern Structure 1 Façade «exposed» cMethodOfFacade() Client «not exposed» This call replaced by 1 & 2. Client can’t reference C 2 «not exposed» C «not exposed» myCMethod() «not exposed» «not exposed» «not exposed»
Sequence Diagram for Façade :Client singleton :Facade :C cMethodOfFacade() myCMethod() (return if any) (return if any)