7 creational design patternweb.cse.msstate.edu/.../7_creational_design_pattern.pdfsoftware...

47
Software Architecture – Creational Design Patterns 1 Chapter 7 Creational Design Patterns

Upload: others

Post on 25-Feb-2021

9 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 1

Chapter 7 Creational Design Patterns

Page 2: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 2

Process Phases Discussed in This Chapter

Requirements Analysis

Design

Implementation

Architecture Framework Detailed Design

x Key: = secondary emphasis x = main emphasis

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 3: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 3

Design Purpose

Create individual objects in situations where the constructor alone is inadequate.

Design Pattern Summary Use methods to return required objects.

Factory

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Objects often aggregate other objects or must belong to subclasses determined at runtime.

Page 4: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 4

Factory Class Model

Factory design pattern

MyClass createObjectOfRequiredClass(): RequiredClass

«create object»

RequiredClass Client

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Note: pattern is a form of delegation. A common motive for using Factory occurs when a base class object is required but the subclass to which it belongs is not known at runtime.

Page 5: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 5

Design Goal At Work: à Reusability and Corrrectness ß

We want to write code about automobiles in general: Code that applies to any make, exercised repeatedly (thus reliably).

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 6: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 6

Application of Factory

design pattern

Factory Example

Ford createAutomobile()

Toyota createAutomobile()

Automobile createAutomobile(): Automobile

Client

«create object» «create object»

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 7: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 7

createObjectOfRequiredClass()

Sequence Diagram for Factory

:MyClass :Client

RequiredClass()

:RequiredClass

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Correct Typing: class Ford extends Automobile { Automobile createAutomobile() { return new Ford() } ...... }

Incorrect Typing: Ford createAutomobile() // return type not identical with // that of the createAutomobile() // we are overriding.

Page 8: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 8

Typical Output of E-Mail Generation Example

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

“From a single version of control code, generate mail messages tailored to various customers.”

Page 9: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 9

Design Goals At Work: à Correctness and Reusability ß

We want to separate the code common to all types of customers. We want to separate the specialized code that generates e-mail for each type of customer. This makes it easier to check for correctness and to reuse parts.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 10: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 10

Application of Factory design pattern

Customer getMessage()

Client sendMessage()

Frequent getMessage()

Returning getMessage()

Curious getMessage()

Newbie getMessage()

MailMessage text

MailGenerationApplication getCustomerTypeFromUser()

«setup»

Factory: Email Generation Example

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 11: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 11

Factory Applied to getGraphics() in Java

Factory design pattern

Component getGraphics(): Graphics

«create object»

Client

Graphics

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

The Java API makes extensive use of Factory. For example, the Factory method getGraphics() in the Java Component API class obtains a graphics object that draws on the specific Component object to which getGraphics() belongs. Box is a Container class allowing display in a horizontal direction or vertical direction. The API provides several facotry methods in Box which create particular kinds of Box objects such as

public static Box createVerticleBox() public static Box createHorizontalBox()

Page 12: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 12

Key Concept: à Singleton Design Pattern ß

-- when a class has exactly one instance.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 13: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 13

Design Purpose

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.

Singleton

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 14: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 14

Design Goal At Work: à Correctness ß

Singleton enforces the intention that only one User object exists, safeguarding the application from unanticipated User instance creation.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 15: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 15

Singleton: Class Model

Singleton Design Pattern

MyClass getSingletonOfMyClass(): MyClass

Client

1

singletonOfMyClass

«static»

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

First, statically define a single instance of class S. Making the constructor of the class MyClass private prevents the creation of MyClass objects except by methods of MyClass itself (in theory).

Page 16: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 16

The Singleton Design Pattern -- applied to MyClass

1.  Define a private static member variable of MyClass of type MyClass

private static MyClass singletonOfMyClass = new MyClass(); 2.  Make the constructor of MyClass private

private MyClass() { /* …. constructor code …. */ }; 3.  Define a public static method to access the

member public static MyClass getSingletonOfMyClass() { return singletonOfMyClass; }

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 17: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 17

Output for Singleton Experiment Example

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

“Prepare to build an application that evaluates the results of lab experiments . The application for this phase does no substantive evaluation, takes no input, and always prints the message above to the console.” “Ensure that there is exactly one Experiment object at runtime. Ensure that it can be accessed by any method in the application, and verify this access by displaying the following at the console. Noting that the experiment singleton referenced n times so far.”

Application of Singleton to Experiment Example

Experiment theExperiment: Experiment

analyze() getTheExperiment(): Experiment

reportResults()

Client theExperiment

1

«static»

Page 18: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 18

Key Concept: à Singleton Design Pattern ß

When a class must have exactly one instance, make the constructor private and the instance a private static variable with a public accessor.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 19: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 19

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.

Abstract Factory

* Gamma et al

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 20: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 20

Abstract Factory*

Abstract Factory Interface

Style….

Client

StyleAFactory StyleBFactory

Ensemble setAbstractFactory()

doAFunction()

AbstractFactory getAPart1Object() getAPart2Object()

* relationships within pattern application not shown

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

For our word processor example, the role of Ensemble would be held by a class such as Document, which supports methods dependent upon Style (our Abstract Factory).

Page 21: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 21

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)

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 22: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 22

Word Processor Interaction 2 of 2: Output Options …. >> Enter the style you want displayed: big

----- Title: MY LIFE -----

Section 1. --- BIRTH --- I was born in a mountain hut …. Section 2. --- YOUTH --- I grew up sturdy … Section 3. --- ADULTHOOD --- ….

…. >> Enter the style you want displayed: small

My Life Birth I was born in a mountain hut …. Youth I grew up sturdy … Adulthood

Option 2 Option 1

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 23: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 23

Application of Abstract Factory

Interface of Abstract Factory Applied to Word Processor

Client

SmallStyle LargeStyle

Style Document setStyle() display()

. . . . . . .

1

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Once the style has been set, the client makes calls to Document such as document.display(). A class model for this would have the appearance illustrated above.

Page 24: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 24

«create» The Abstract Factory Idea

Ensemble setAbstractFactory()

doAFunction()

AbstractFactory getAPart1Object() getAPart2Object()

StyleAFactory getAPart1Object() getAPart2Object()

Part1 Part2

Part1StyleA Part2StyleA

abstractFactory 1

Client

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Ensemble, a partial class model shows how Ensemble objects are constructed in a style encapsulated as StyleA.

Page 25: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 25

«create»

Abstract Factory (Full class model with two styles)

Style….

AbstractFactory getAPart1Object() getAPart2Object()

StyleAFactory StyleBFactory

Part1 Part2

Part1StyleA Part1StyleB Part2StyleA Part2StyleB

abstractFactory 1 Ensemble doAFunction()

Client

1..n 1..n

Part…

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 26: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 26

} }

Sequence Diagram

for Abstract Factory

StyleXFactory()

:Client abstractFactory :StyleXFactory

:Ensemble

setAbstractFactory (abstractFactory )

getAPart_i()

Part_iStyleX()

Selecting a style

Creating a Part_i object in required style

getAPart_i()

abstractFactory :AbstractFactory

:Part_iStyleX

doAFunction()

Virtual function property

Assume that this method requires a Part_i object.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 27: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 27

Design Goals At Work: à 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.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 28: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 28

Abstract Factory Applied to Word Processor

Document getAHeading()

getATitle() grtDocumentFromUser()

«create» SmallStyle getAHeading()

getATitle()

LargeStyle getAHeading()

getATitle()

Title value

Heading value

LargeHeading display()

SmallHeading display()

LargeTitle display()

SmallTitle display()

Text value

1

0..n 0..n

Style getAHeading()

getATitle()

Displayable

Displayable display()

Client getStyleFromUser() displayDocument()

style

Look at main() in Setup. It first gets the doc content from the user then creates a Document object.

It then prompts the user for the style required, and sets the style attribute of the document accordingly. Finally it activates the client to interact with the document.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 29: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 29

“Word Processor” Interaction

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 30: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 30

An Abstract Factory Application: Java ToolKit

ToolKit createButton() createImage()

ConcreteToolkit1 createButton() createImage()

ConcreteToolkit2 createButton() createImage()

ButtonPeer

ConcreteToolkit3 createButton() createImage()

ButtonPeer implementation 3

ImagePeer implementation 3

ImagePeer

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 31: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 31

Abstract Factory: Narrow Interface

Style….

AbstractFactory getAPart1Object() getAPart2Object()

StyleAFactory StyleBFactory

abstractFactory 1 Ensemble setStyleA() setStyleB()

Client

. . . .

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

The interface can be made more restrictive, where Client references only Ensemble. In this case, a setup part of the application deals with instantiating the “style” (abstract factory object).

Page 32: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 32

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.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 33: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 33

Prototype Design Example:

A Selection

Click on choice of storage:

Click on choice of chair:

Click on choice of desk: Furniture color

Furniture hardware

type

colonial

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 34: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 34

A Simplified Prototype Example

Page 35: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 35

Design Purpose

Create a set of almost identical objects whose type is determined at runtime.

Design Pattern

Assume that a prototype instance is known; clone it whenever a new instance is needed.

Prototype

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 36: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 36

Prototype Interface With Clients

Ensemble createEnsemble()

Client

Part1 clone()

Part2 clone()

(optional part of interface)

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

clone() delegates object construction to a constructor

Page 37: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 37

Prototype Design Pattern

The Prototype Idea

Ensemble createEnsemble()

Client

MyPart clone(): MyPart

MyPartStyleA clone()

MyPartStyleB clone()

myPartPrototype 1

// To create a MyPart instance: MyPart p = myPartPrototype.clone();

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 38: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 38

Prototype Class Model

Ensemble createEnsemble()

Client

Part1 clone()

Part2 clone()

Part1StyleA clone()

Part1StyleB clone()

Part2StyleA clone()

Part2StyleB clone()

part1Prototype part2Prototype

1 1

..... // To create a Part1 object: Part1 p1 = part1Prototype.clone(); ….

Part1StyleB returnObject = new Part1StyleB(); ….

Part1StyleC clone()

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 39: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 39

Sequence Diagram for Prototype

partNPrototype :PartN

:Ensemble

createEnsemble()

clone()

partNPrototype :PartNStyleB

:PartNStyleB

PartNStyleB()

(virtual function property)

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 40: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 40

Design Goals At Work: à 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.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 41: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 41

Customer Information Entry: Typical Scenario

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 42: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 42

OfficeProcess doAProcess()

Prototype Example Outline

HiVolCustomer clone()

LoVolCustomer clone()

Customer clone(): Customer

customerPrototype

Client

1

MedVolCustomer clone()

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 43: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 43

Requirement for (Deep) Cloning

Class1 Class2

c1:Class1 c2:Class2 (2) c1 an instance of Class1:

c1.clone should be as follows (deep clone):

c1.clone:Class1 x*:Class2

* a clone of c2

In shallow cloning, c1.clone actually as follows!

c1.clone:Class1

Given:

(1) Class model:

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 44: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 44

Output for Demonstration of Shallow Cloning

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 45: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 45

Output for Demonstration of Deep Cloning

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 46: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 46

Key Concept: à Prototype Pattern ß

-- when designing for multiple instances which are the same in key respects, create them by cloning a prototype.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 47: 7 Creational Design Patternweb.cse.msstate.edu/.../7_Creational_Design_Pattern.pdfSoftware Architecture – Creational Design Patterns 4 Factory Class Model Factory design pattern

Software Architecture – Creational Design Patterns 47

Summary of Creational Patterns

Use Creational Design Patterns when creating complex objects

•  Factory when creating individuals

•  Abstract Factory when creating families

•  Prototype to “mix & match”

•  Singleton for exactly one, safely

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.