design patterns creational patterns

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

Upload: malik-sajid

Post on 07-Apr-2017

111 views

Category:

Engineering


5 download

TRANSCRIPT

Page 1: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

Obj

ectiv

esLecture 13

Creational Design Pattern

SWE 316: Software Design and Architecture

To learn the creational design patterns and when to use them.

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

Page 2: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

Creational design patterns to be coveredCreational design patterns:

Singleton

Factory

Abstract factory

Prototype

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 2/27

Page 3: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

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.

ProblemApplication needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary.

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

n Factory Abstract Factory

Prototype

Summary 3/27

Page 4: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

Singleton applicability 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.

KEY CONCEPTDesign Goal: Correctness

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 4/27

Page 5: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

Singleton consequences Singleton has following benefits:

Controlled access to sole instance.

Permits a variable number of instances.

More flexible than class operations.

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 5/27

Page 6: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

Singleton: Class Model

MyClass getSingletonOfMyClass(): MyClass

Client

1

singletonOfMyClass

«static»

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 6/27

Page 7: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

Singleton: Sample code

7

Define a private static member variable of type MyClass1

Make the constructor of MyClass private2

Define a public static method to access the member

3

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 7/27

Page 8: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

Comments on Singleton 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.

KEY CONCEPTSingleton Design Pattern

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 8/27

Page 9: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

Factory Design Pattern Design PurposeCreate individual objects in situations where the constructor alone is inadequate.

Design Pattern SummaryUse methods to return required objects.

7.2Singleto

nFactor

yAbstract Factory

Prototype

Summary 9/27

Page 10: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

Factory Class Model

Factory design pattern

MyClass createObjectOfRequiredClass(): RequiredClass

«create object»

RequiredClassClient

Singleton

Factory

Abstract Factory

Prototype

Summary 10/27

Page 11: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

Applicationof Factory

designpattern

Factory Example (Figure 7.4)

Ford createAutomobile()

Toyota createAutomobile()

Automobile createAutomobile(): Automobile

Client

«create object» «create object»

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

KEY CONCEPTDesign Goal : Reusability and Correctness

Singleton

Factory

Abstract Factory

Prototype

Summary 11/27

Page 12: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

Application of Factory design pattern

ClientsendMessage()

Customer getMessage()

Frequent getMessage()

Returning getMessage()

Curious getMessage()

Newbie getMessage()

MailMessagetext

MailGenerationApplicationgetCustomerTypeFromUser()

«setup»

Factory: Email Generation Example

Singleton

Factory

Abstract Factory

Prototype

Summary 12/27

Page 13: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

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.

Singleton

Factory

Abstract Factory

Prototype

Summary 13/27

Page 14: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

Abstract Factory

Design Purpose“Provide an interface for creating families of related or dependent objects without specifying their concrete classes.”*

Design PatternCapture family creation in a class containing a factory method for each class in the family.

* Gamma et al

7.4Singleto

n Factory Abstract Factory

Prototype

Summary 14/27

Page 15: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

Abstract Factory*

Abstract Factory Interface (Figure 7.17)

Style….

Client

StyleAFactory StyleBFactory

EnsemblesetAbstractFactory()

doAFunction()

AbstractFactorygetAPart1Object()getAPart2Object()

* relationships within pattern application not shown

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 15/27

Page 16: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

Application of Abstract Factory

Interface of Abstract Factory Applied to Word Processor

Client

SmallStyle LargeStyle

StyleDocumentsetStyle()display()

. . . . . . .

1

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 16/27

Page 17: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

«create»

The Abstract Factory Idea (Figure 7.19)

AbstractFactorygetAPart1Object()getAPart2Object()

StyleAFactorygetAPart1Object()getAPart2Object()

Part1StyleA Part2StyleA

Part1 Part2

abstractFactory 1Ensemble

setAbstractFactory()doAFunction()

Client

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 17/27

Page 18: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

«create»

Abstract Factory (Figure 7.20)

Style….

AbstractFactorygetAPart1Object()getAPart2Object()

StyleAFactory StyleBFactory

Part1StyleA Part1StyleB Part2StyleA Part2StyleB

Part1 Part2

abstractFactory 1Ensemble

doAFunction()

Client

1..n1..n

Part…

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 18/27

Page 19: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

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.

KEY CONCEPTDesign Goals : Correctness and Reusability

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 19/27

Page 20: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

To design an application in which there are several possible styles for a collection of objects, capture styles as classes with coordinated factory methods.

KEY CONCEPTAbstract Factory Design Pattern

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 20/27

Page 21: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

Prototype Design 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.

KEY CONCEPTPrototype Pattern

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 21/27

Page 22: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

Prototype Design Example: A SelectionGraphics courtesy COREL

Click on choice of storage:Click on choice of chair:

Click on choice of desk:Furniture

color

Furniture

hardware typecoloni

al

Adapted from Software Design: From Programming to

Architecture by Eric J. Braude (Wiley 2003), with permission.

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 22/27

Page 23: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

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.

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 23/27

Page 24: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

The Prototype Idea

EnsemblecreateEnsemble()

Client

MyPartclone(): MyPart

MyPartStyleAclone()

MyPartStyleBclone()

myPartPrototype 1

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

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 24/27

Page 25: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

Prototype Class Model

EnsemblecreateEnsemble()

Client

Part1StyleAclone()

Part1StyleBclone()

Part2StyleAclone()

Part2StyleBclone()

Part1clone()

Part2clone()

part1Prototype part2Prototype

1 1

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

Part1StyleB returnObject = new Part1StyleB();….

Part1StyleCclone()

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 25/27

Page 26: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

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.

KEY CONCEPTDesign Goals : Correctness and Reusability

Singleton Factory Abstract

FactoryPrototyp

eSummar

y 26/27

Page 27: Design patterns creational patterns

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser

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

Prototype to “mix & match”

Singleton Factory Abstract

FactoryPrototyp

eSummar

y27/27