1 / 27

Lecture 13 Creational Design Pattern

SWE 316: Software Design and Architecture. Lecture 13 Creational Design Pattern. Ch 7. To learn the creational design patterns and when to use them. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission. 2/27. Singleton. Factory.

dalit
Download Presentation

Lecture 13 Creational Design Pattern

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. SWE 316: Software Design and Architecture Lecture 13Creational Design Pattern Ch 7 • To learn the creational design patterns and when to use them. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  2. 2/27 Singleton Factory Abstract Factory Prototype Summary Creational design patterns to be covered • Creational design patterns: • Singleton • Factory • Abstract factory • Prototype

  3. 3/27 Singleton Factory Abstract Factory Prototype Summary Singleton Design Pattern • Intent/ Design Purpose • when a class has exactly one instance. • Ensure that there is exactly one instance of a class S. Be able to obtain the instance 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. 7.3

  4. 4/27 Singleton Factory Abstract Factory Prototype Summary Singleton applicability KEY CONCEPT Design Goal: Correctness • Use the singleton pattern when • There must be exactly one instance of a class, and it must be accessible to client from a well-known access point. • When the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code. Singleton enforces the intention that only one User object exists, safeguarding the application from unanticipated User instance creation.

  5. 5/27 Singleton Factory Abstract Factory Prototype Summary Singleton consequences • Singleton has following benefits: • Controlled access to sole instance. • Permits a variable number of instances. • More flexible than class operations.

  6. 6/27 Singleton Factory Abstract Factory Prototype Summary Singleton: Class Model Client singletonOfMyClass MyClass getSingletonOfMyClass(): MyClass «static» 1

  7. 7/27 Singleton Factory Abstract Factory Prototype Summary Singleton: Sample code 2 3 Make the constructor of MyClass private Define a public static method to access the member 1 Define a private static member variable of type MyClass

  8. 8/27 Singleton Factory Abstract Factory Prototype Summary Comments on Singleton KEY CONCEPT Singleton Design Pattern • This form of Singleton is simple but it creates the Singleton object even if the object is never needed; this is wasteful if Singleton is large. • The idea of Singleton can be extended to the problem of having just two instances of a class. When a class must have exactly one instance, make the constructor private and the instance a private static variable with a public accessor.

  9. 9/27 Singleton Factory Abstract Factory Prototype Summary Factory Design Pattern • Design Purpose Create individual objects in situations where the constructor alone is inadequate. • Design Pattern Summary Use methods to return required objects. 7.2

  10. 10/27 Singleton Factory Abstract Factory Prototype Summary Factory Class Model Client RequiredClass «create object» Factory design pattern MyClass createObjectOfRequiredClass(): RequiredClass

  11. 11/27 Singleton Factory Abstract Factory Prototype Summary Factory Example (Figure 7.4) KEY CONCEPT Design Goal :Reusability and Correctness Client We want to write code about automobiles in general: Code that applies to any make, exercised repeatedly (thus reliably). Automobile createAutomobile(): Automobile Application of Factory design pattern Ford createAutomobile() Toyota createAutomobile() «create object» «create object»

  12. 12/27 Singleton Factory Abstract Factory Prototype Summary Factory: Email Generation Example Customer getMessage() Application of Factory design pattern Frequent getMessage() Returning getMessage() Curious getMessage() Newbie getMessage() Client sendMessage() «setup» MailGenerationApplication getCustomerTypeFromUser() MailMessage text

  13. 13/27 Singleton Factory Abstract Factory Prototype Summary Comments on Factory • Applications of Factory have been increasingly common in API’s because they improve robustness by ensuring that objects created respect necessary constraints.

  14. 14/27 Singleton Factory Abstract Factory Prototype Summary 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 containing a factory method for each class in the family. 7.4 * Gamma et al

  15. 15/27 Singleton Factory Abstract Factory Prototype Summary Abstract Factory Interface (Figure 7.17) Client Abstract Factory* AbstractFactory getAPart1Object() getAPart2Object() Ensemble setAbstractFactory() doAFunction() StyleAFactory StyleBFactory Style…. * relationships within pattern application not shown

  16. 16/27 Singleton Factory Abstract Factory Prototype Summary Interface of Abstract Factory Applied to Word Processor Application of Abstract Factory 1 Document setStyle() display() Style SmallStyle LargeStyle . . . . . . . Client

  17. 17/27 Singleton Factory Abstract Factory Prototype Summary The Abstract Factory Idea (Figure 7.19) AbstractFactory getAPart1Object() getAPart2Object() Ensemble setAbstractFactory() doAFunction() 1 abstractFactory Part1 Part2 Part1StyleA Part2StyleA StyleAFactory getAPart1Object() getAPart2Object() «create» Client

  18. 18/27 Singleton Factory Abstract Factory Prototype Summary Abstract Factory (Figure 7.20) AbstractFactory getAPart1Object() getAPart2Object() Ensemble doAFunction() 1 abstractFactory Part1 Part2 1..n 1..n Part… Part1StyleA Part1StyleB Part2StyleA Part2StyleB «create» StyleAFactory StyleBFactory Style…. Client

  19. 19/27 Singleton Factory Abstract Factory Prototype Summary KEY CONCEPT Design Goals : Correctness and Reusability We want to separate the code parts that format the document in each style. We also want to separate the common document generation code. This facilitates reusing parts and checking for correctness.

  20. 20/27 Singleton Factory Abstract Factory Prototype Summary KEY CONCEPT Abstract Factory Design Pattern To design an application in which there are several possible styles for a collection of objects, capture styles as classes with coordinated factory methods.

  21. 21/27 Singleton Factory Abstract Factory Prototype Summary Prototype Design Pattern KEY CONCEPT Prototype Pattern • Design Purpose • Create a set of almost identical objects whose type is determined at runtime. • Assume that a prototype instance is known; clone it whenever a new instance is needed. -- when designing for multiple instances which are the same in key respects, create them by cloning a prototype.

  22. 22/27 Singleton Factory Abstract Factory Prototype Summary Prototype Design Example: A Selection Graphics courtesy COREL Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission. Furniture color Click on choice of desk: Furniture hardware type Click on choice of storage: colonial Click on choice of chair:

  23. 23/27 Singleton Factory Abstract Factory Prototype Summary Prototype consequences • It hides the concrete product classes from the client. • It let client work with application-specific classes without modification. • It adds and removes products at run-time. • It configures an application with classes dynamically.

  24. 24/27 Singleton Factory Abstract Factory Prototype Summary The Prototype Idea Client myPartPrototype 1 Ensemble createEnsemble() MyPart clone(): MyPart // To create a MyPart instance: MyPart p = myPartPrototype.clone(); MyPartStyleB clone() MyPartStyleA clone()

  25. 25/27 Singleton Factory Abstract Factory Prototype Summary Prototype Class Model Client ..... // To create a Part1 object: Part1 p1 = part1Prototype.clone(); …. Ensemble createEnsemble() part1Prototype part2Prototype Part1 clone() Part2 clone() 1 1 Part1StyleA clone() Part1StyleB clone() Part2StyleA clone() Part2StyleB clone() Part1StyleC clone() Part1StyleB returnObject = new Part1StyleB(); ….

  26. 26/27 Singleton Factory Abstract Factory Prototype Summary KEY CONCEPT Design Goals : Correctness and Reusability We want to isolate the parts pertaining to each type of customer. We also want to isolate the common customer code. This makes it easier to check the design and implementation for correctness, and to reuse the parts.

  27. 27/27 Singleton Factory Abstract Factory Prototype Summary Summary of Creational Patterns • Use Creational Design Patterns when creating complex objects • Singleton • for exactly one, safely • when a class has exactly one instance • Factory when creating individuals • Abstract Factory when creating families • Prototypeto “mix & match”

More Related