1 / 90

Software Design Patterns

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?

shlomo
Télécharger la présentation

Software 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. Software Design Patterns Main Reference: Software Engineering: Modern Approaches, 2nd ed., 2010, Wiley. Chapter 17 Eric Braude and Michael Bernstein

  2. 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?

  3. 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

  4. KitchenViewer Interface Wall cabinet menu  Counter display area styles Floor cabinet Modern Classic Antique Arts & Crafts

  5. KitchenViewer Example Wall cabinets Floor cabinets Countertop Modern Classic Antique Arts & Crafts

  6. Selecting Antique Style Modern Classic Antique Arts & Crafts

  7. KitchenViewer Without Design Patterns Client renderKitchen() Kitchen WallCabinet FloorCabinet ModernWallCabinet AntiqueWallCabinet ModernFloorCabinet AntiqueFloorCabinet

  8. 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(); }

  9. 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

  10. Abstract Factory Design Pattern Client doOperation( Style myStyle ) Style getComponentA() getComponentB() Collection ComponentA ComponentB Style1ComponentA Style1 getComponentA() getComponentB() Style2ComponentA Style2 getComponentA() getComponentB() Style1ComponentB Style2ComponentB

  11. Abstract Factory Design PatternAlternative Client doOperation() Collection getComponentA() getComponentB() Style getComponentA() getComponentB() ComponentA ComponentB Style1ComponentA Style1 getComponentA() getComponentB() Style2ComponentA Style2 getComponentA() getComponentB() Style1ComponentB Style2ComponentB

  12. Example of Behavioral Design Goal: Port Traffic obstacles Algorithms express mutual behavior to drydock  berth berth

  13. Avoiding Dependencies Harbor Application  1 1..n Ship Tugboat Customs application: reuse Ship alone Ship Longshoreman

  14. Core Mediator Concept Applied to The Harbor Problem Ship LeavingPort estimateTime() Tugboat

  15. Applying the Mediator Design Pattern to The Harbor Problem PortMission estimateTime() Vessel Mediator base class Ship Tugboat EnteringPort estimateTime() LeavingPort estimateTime() BeingMaintained estimateTime()

  16. 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

  17. (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)

  18. (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)

  19. Client myStyle:KitchenStyle Abstract Factory Application Sequence Diagram myStyle. getWallCabinet() -- IF myStyle BELONGS TO ModernKStyle -- myStyle: ModernKStyle wallCabinet1: ModernWallCabinet renderKitchen ( myStyle ) getWallCabinet() ModernWallCabinet()

  20. 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()

  21. Concrete and Abstract Layers Client Abstract level Concrete level

  22. Concrete and Abstract Layers Client WallCabinet KitchenStyle FloorCabinet Abstract level Concrete level Kitchen ModernWallCabinet AntiqueWallCabinet ModernKStyle ModernFloorCabinet AntiqueKStyle AntiqueFloorCabinet

  23. The Three Roles Involved in Design Pattern Usage 2. Client Role 1. Design Pattern Application 3. Setup Role Rest of the Application

  24. 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

  25. 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.

  26. 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.

  27. Singleton: Class Model Client Singleton Design Pattern singletonOfMyClass MyClass getSingletonOfMyClass(): MyClass «static» 1

  28. 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; • }

  29. Output for Singleton Experiment Example

  30. Application of Singleton to Experiment Example Experiment theExperiment: Experiment analyze() getTheExperiment(): Experiment reportResults() theExperiment Client «static» 1

  31. 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)

  32. 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

  33. Abstract Factory Design Purpose “Provide an interface for creating families of related or dependent objects without specifying their concrete classes.”* * Gamma et al

  34. 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

  35. Abstract Factory Interface Client Abstract Factory* AbstractFactory getAPart1Object() getAPart2Object() Ensemble setAbstractFactory() doAFunction() StyleAFactory StyleBFactory Style…. * relationships within pattern application not shown

  36. Interface of Abstract Factory Applied to Word Processor Application of Abstract Factory 1 Document setStyle() display() Style SmallStyle LargeStyle . . . . . . . Client

  37. The Abstract Factory Idea AbstractFactory getAPart1Object() getAPart2Object() abstractFactory 1 Ensemble setAbstractFactory() doAFunction() Part1 Part2 Part1StyleA Part2StyleA StyleAFactory getAPart1Object() getAPart2Object() «create» Client

  38. 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

  39. 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

  40. 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()

  41. “Word Processor” Interaction

  42. 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.

  43. Facade Design Pattern Structure 1 Façade «exposed» cMethodOfFacade() Client 2 C «not exposed» myCMethod()

  44. 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»

  45. Sequence Diagram for Façade :Client singleton :Facade :C cMethodOfFacade() myCMethod() (return if any) (return if any)

More Related