SlideShare a Scribd company logo
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 7Adapted from Software Design: From Programming
to Architecture by Eric J. Braude (Wiley 2003), with
permission.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Creational design patterns to be covered
 Creational design patterns:
 Singleton
 Factory
 Abstract factory
 Prototype
Singleton Factory Abstract Factory Prototype Summary 2/27
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.
 Problem
Application 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.3
Singleton Factory Abstract Factory Prototype Summary 3/27
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 CONCEPT
Design Goal: Correctness
Singleton Factory Abstract Factory Prototype Summary 4/27
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 Factory Prototype Summary 5/27
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Singleton: Class Model
MyClass
getSingletonOfMyClass(): MyClass
Client
1
singletonOfMyClass
«static»
Singleton Factory Abstract Factory Prototype Summary 6/27
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Singleton: Sample code
7
Define a private static member
variable of type MyClass
1
Make the constructor of MyClass private2
Define a public
static method to
access the member
3
Singleton Factory Abstract Factory Prototype Summary 7/27
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 CONCEPT
Singleton Design Pattern
Singleton Factory Abstract Factory Prototype Summary 8/27
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
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
Singleton Factory Abstract Factory Prototype Summary 9/27
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
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Application
of Factory
design
pattern
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 CONCEPT
Design Goal : Reusability and Correctness
Singleton Factory Abstract Factory Prototype Summary 11/27
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Application of Factory design pattern
Client
sendMessage()
Customer
getMessage()
Frequent
getMessage()
Returning
getMessage()
Curious
getMessage()
Newbie
getMessage()
MailMessage
text
MailGenerationApplication
getCustomerTypeFromUser()
«setup»
Factory: Email Generation Example
Singleton Factory Abstract Factory Prototype Summary 12/27
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
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 Pattern
Capture family creation in a class containing a factory
method for each class in the family.
* Gamma et al
7.4
Singleton Factory Abstract Factory Prototype Summary 14/27
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Abstract Factory*
Abstract Factory Interface (Figure 7.17)
Style….
Client
StyleAFactory StyleBFactory
Ensemble
setAbstractFactory()
doAFunction()
AbstractFactory
getAPart1Object()
getAPart2Object()
* relationships within pattern application not shown
Singleton Factory Abstract Factory Prototype Summary 15/27
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Application of Abstract Factory
Interface of Abstract Factory Applied to Word
Processor
Client
SmallStyle LargeStyle
StyleDocument
setStyle()
display()
. . . . . . .
1
Singleton Factory Abstract Factory Prototype Summary 16/27
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
«create»
The Abstract Factory Idea (Figure 7.19)
AbstractFactory
getAPart1Object()
getAPart2Object()
StyleAFactory
getAPart1Object()
getAPart2Object()
Part1StyleA Part2StyleA
Part1 Part2
abstractFactory 1
Ensemble
setAbstractFactory()
doAFunction()
Client
Singleton Factory Abstract Factory Prototype Summary 17/27
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
«create»
Abstract Factory (Figure 7.20)
Style….
AbstractFactory
getAPart1Object()
getAPart2Object()
StyleAFactory StyleBFactory
Part1StyleA Part1StyleB Part2StyleA Part2StyleB
Part1 Part2
abstractFactory 1
Ensemble
doAFunction()
Client
1..n1..n
Part…
Singleton Factory Abstract Factory Prototype Summary 18/27
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 CONCEPT
Design Goals : Correctness and Reusability
Singleton Factory Abstract Factory Prototype Summary 19/27
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 CONCEPT
Abstract Factory Design Pattern
Singleton Factory Abstract Factory Prototype Summary 20/27
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 CONCEPT
Prototype Pattern
Singleton Factory Abstract Factory Prototype Summary 21/27
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Prototype Design Example: A Selection
Graphics courtesy COREL
Click on choice of storage:
Click on choice of chair:
Click on choice of desk:Furnit
ure
color
Furnitu
re
hardwa
re typecoloni
al
Adapted from Software Design:
From Programming to
Architecture by Eric J. Braude
(Wiley 2003), with permission.
Singleton Factory Abstract Factory Prototype Summary 22/27
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 Factory Prototype Summary 23/27
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
The Prototype Idea
Ensemble
createEnsemble()
Client
MyPart
clone(): MyPart
MyPartStyleA
clone()
MyPartStyleB
clone()
myPartPrototype 1
// To create a MyPart instance:
MyPart p = myPartPrototype.clone();
Singleton Factory Abstract Factory Prototype Summary 24/27
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Prototype Class Model
Ensemble
createEnsemble()
Client
Part1StyleA
clone()
Part1StyleB
clone()
Part2StyleA
clone()
Part2StyleB
clone()
Part1
clone()
Part2
clone()
part1Prototype part2Prototype
1 1
.....
// To create a Part1 object:
Part1 p1 = part1Prototype.clone();
….
Part1StyleB returnObject = new Part1StyleB();
….
Part1StyleC
clone()
Singleton Factory Abstract Factory Prototype Summary 25/27
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 CONCEPT
Design Goals : Correctness and Reusability
Singleton Factory Abstract Factory Prototype Summary 26/27
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 Factory Prototype Summary 27/27

More Related Content

PPTX
Creational pattern
Himanshu
 
PPT
Fundamentals of JAVA
KUNAL GADHIA
 
PPT
Design Patterns
Anuja Arosha
 
PDF
Introduction to Design Pattern
Sanae BEKKAR
 
PPT
Oops in Java
malathip12
 
PPTX
Gof design patterns
Srikanth R Vaka
 
PPT
Prototype pattern
Shakil Ahmed
 
PPT
Introduction to c#
OpenSource Technologies Pvt. Ltd.
 
Creational pattern
Himanshu
 
Fundamentals of JAVA
KUNAL GADHIA
 
Design Patterns
Anuja Arosha
 
Introduction to Design Pattern
Sanae BEKKAR
 
Oops in Java
malathip12
 
Gof design patterns
Srikanth R Vaka
 
Prototype pattern
Shakil Ahmed
 

What's hot (20)

PPT
Abstract data types
Luis Goldster
 
PPTX
Awt, Swing, Layout managers
swapnac12
 
PPT
Object and class relationships
Pooja mittal
 
PPTX
Introduction to object-oriented analysis and design (OOA/D)
Ahmed Farag
 
PPTX
Concurrency control
Soumyajit Dutta
 
PDF
Object-Oriented Analysis And Design With Applications Grady Booch
Sorina Chirilă
 
PPTX
Design Pattern - Singleton Pattern
Mudasir Qazi
 
PPTX
Sqlite
Raghu nath
 
PPTX
.Net Assemblies
Muhammad Kamran Rafi
 
ODP
OOP java
xball977
 
PPT
Composite pattern
Shakil Ahmed
 
PPTX
Oops in vb
Dalwin INDIA
 
PPTX
Arrays in Java
Abhilash Nair
 
PDF
Design patterns tutorials
University of Technology
 
PPTX
Singleton Design Pattern - Creation Pattern
Seerat Malik
 
PPT
Singleton design pattern
11prasoon
 
PPT
Builder pattern
Shakil Ahmed
 
PPTX
Model driven architecture
Biruk Mamo
 
PPT
Java collection
Arati Gadgil
 
PPTX
Object Oriented Approach for Software Development
Rishabh Soni
 
Abstract data types
Luis Goldster
 
Awt, Swing, Layout managers
swapnac12
 
Object and class relationships
Pooja mittal
 
Introduction to object-oriented analysis and design (OOA/D)
Ahmed Farag
 
Concurrency control
Soumyajit Dutta
 
Object-Oriented Analysis And Design With Applications Grady Booch
Sorina Chirilă
 
Design Pattern - Singleton Pattern
Mudasir Qazi
 
Sqlite
Raghu nath
 
.Net Assemblies
Muhammad Kamran Rafi
 
OOP java
xball977
 
Composite pattern
Shakil Ahmed
 
Oops in vb
Dalwin INDIA
 
Arrays in Java
Abhilash Nair
 
Design patterns tutorials
University of Technology
 
Singleton Design Pattern - Creation Pattern
Seerat Malik
 
Singleton design pattern
11prasoon
 
Builder pattern
Shakil Ahmed
 
Model driven architecture
Biruk Mamo
 
Java collection
Arati Gadgil
 
Object Oriented Approach for Software Development
Rishabh Soni
 
Ad

Viewers also liked (10)

PPTX
L'acte zen du jour : ralentir votre rythme de vie
Olivier Perrussel
 
PDF
Untitleddocument
Randy King
 
PPTX
Kinder 3ro
nancy herrera
 
DOCX
The Path to Success-2
Brandon Fischer
 
PDF
Rangkuman pedoman pkm 2016
Fitri Wakhida Yusro
 
PDF
CV for Clifford K. Msumba IT
Clifford Kudakwashe Msumba
 
PDF
NRG4SD_-_Subnat_Govts_at_Forefront_of_Climate_Action-2
Maruxa Cardama
 
PDF
Application for post of Rig Manager 1
Grennel Pereira
 
PPTX
Впровадження здровязберігаючих технологій
ЗШ № 8
 
PDF
Lithiase urinaire: épidémiologie et traitements
Actualité de la médecine
 
L'acte zen du jour : ralentir votre rythme de vie
Olivier Perrussel
 
Untitleddocument
Randy King
 
Kinder 3ro
nancy herrera
 
The Path to Success-2
Brandon Fischer
 
Rangkuman pedoman pkm 2016
Fitri Wakhida Yusro
 
CV for Clifford K. Msumba IT
Clifford Kudakwashe Msumba
 
NRG4SD_-_Subnat_Govts_at_Forefront_of_Climate_Action-2
Maruxa Cardama
 
Application for post of Rig Manager 1
Grennel Pereira
 
Впровадження здровязберігаючих технологій
ЗШ № 8
 
Lithiase urinaire: épidémiologie et traitements
Actualité de la médecine
 
Ad

Similar to Design patterns creational patterns (20)

PPTX
PATTERNS02 - Creational Design Patterns
Michael Heron
 
PPTX
Sda 8
AmberMughal5
 
PPTX
Creational Patterns
Asma CHERIF
 
PPTX
Lecture-7.pptx software design and Arthitechure
MuhammadAbubakar114879
 
PPTX
OOPSDesign PPT ( introduction to opps and design (
bhfcvh531
 
PPT
Introduction to design_patterns
amitarcade
 
PPTX
CREATIONAL Pattern .pptx
Roland Ivan Palasigue II
 
PPTX
design patter related ppt and presentation
Indu32
 
PPT
Unit 2-Design Patterns.ppt
MsRAMYACSE
 
PPTX
Creational Design Patterns.pptx
Sachin Patidar
 
PPT
Design Pattern For C# Part 1
Shahzad
 
PPT
Software Design Patterns
Pankhuree Srivastava
 
PPTX
note2_DesignPatterns (1).pptx
ReemaAsker1
 
PDF
E1803023637
IOSR Journals
 
PPT
P Training Presentation
Gaurav Tyagi
 
PDF
The maze of Design Patterns & SOLID Principles
Muhammad Raza
 
PPTX
Software Architecture and Design Patterns Notes.pptx
VivekanandaGN2
 
PDF
Design patterns in Java - Monitis 2017
Arsen Gasparyan
 
PPT
Design Patterns
Rafael Coutinho
 
PPTX
Cs 1023 lec 8 design pattern (week 2)
stanbridge
 
PATTERNS02 - Creational Design Patterns
Michael Heron
 
Creational Patterns
Asma CHERIF
 
Lecture-7.pptx software design and Arthitechure
MuhammadAbubakar114879
 
OOPSDesign PPT ( introduction to opps and design (
bhfcvh531
 
Introduction to design_patterns
amitarcade
 
CREATIONAL Pattern .pptx
Roland Ivan Palasigue II
 
design patter related ppt and presentation
Indu32
 
Unit 2-Design Patterns.ppt
MsRAMYACSE
 
Creational Design Patterns.pptx
Sachin Patidar
 
Design Pattern For C# Part 1
Shahzad
 
Software Design Patterns
Pankhuree Srivastava
 
note2_DesignPatterns (1).pptx
ReemaAsker1
 
E1803023637
IOSR Journals
 
P Training Presentation
Gaurav Tyagi
 
The maze of Design Patterns & SOLID Principles
Muhammad Raza
 
Software Architecture and Design Patterns Notes.pptx
VivekanandaGN2
 
Design patterns in Java - Monitis 2017
Arsen Gasparyan
 
Design Patterns
Rafael Coutinho
 
Cs 1023 lec 8 design pattern (week 2)
stanbridge
 

Recently uploaded (20)

PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
VinayB68
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
PDF
ETO & MEO Certificate of Competency Questions and Answers
Mahmoud Moghtaderi
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Dr. Rahul Kumar
 
PDF
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
PPTX
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
shravanidabhane8
 
PDF
Structs to JSON How Go Powers REST APIs.pdf
Emily Achieng
 
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
sangeethamtech26
 
PPTX
Module_II_Data_Science_Project_Management.pptx
anshitanarain
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PDF
July 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PDF
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
PDF
A Framework for Securing Personal Data Shared by Users on the Digital Platforms
ijcncjournal019
 
PPTX
Production of bioplastic from fruit peels.pptx
alwingeorgealwingeor
 
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
ghousebhasha2007
 
PDF
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
Ajaykumar966781
 
PDF
Queuing formulas to evaluate throughputs and servers
gptshubham
 
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
VinayB68
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
ETO & MEO Certificate of Competency Questions and Answers
Mahmoud Moghtaderi
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Dr. Rahul Kumar
 
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
shravanidabhane8
 
Structs to JSON How Go Powers REST APIs.pdf
Emily Achieng
 
Strings in CPP - Strings in C++ are sequences of characters used to store and...
sangeethamtech26
 
Module_II_Data_Science_Project_Management.pptx
anshitanarain
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
July 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
A Framework for Securing Personal Data Shared by Users on the Digital Platforms
ijcncjournal019
 
Production of bioplastic from fruit peels.pptx
alwingeorgealwingeor
 
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
ghousebhasha2007
 
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
Ajaykumar966781
 
Queuing formulas to evaluate throughputs and servers
gptshubham
 

Design patterns creational patterns

  • 1. 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 7Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
  • 2. SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Creational design patterns to be covered  Creational design patterns:  Singleton  Factory  Abstract factory  Prototype Singleton Factory Abstract Factory Prototype Summary 2/27
  • 3. 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.  Problem Application 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.3 Singleton Factory Abstract Factory Prototype Summary 3/27
  • 4. 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 CONCEPT Design Goal: Correctness Singleton Factory Abstract Factory Prototype Summary 4/27
  • 5. 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 Factory Prototype Summary 5/27
  • 6. SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Singleton: Class Model MyClass getSingletonOfMyClass(): MyClass Client 1 singletonOfMyClass «static» Singleton Factory Abstract Factory Prototype Summary 6/27
  • 7. SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Singleton: Sample code 7 Define a private static member variable of type MyClass 1 Make the constructor of MyClass private2 Define a public static method to access the member 3 Singleton Factory Abstract Factory Prototype Summary 7/27
  • 8. 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 CONCEPT Singleton Design Pattern Singleton Factory Abstract Factory Prototype Summary 8/27
  • 9. SWE 316: Software Design and Architecture – Dr. Khalid Aljasser 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 Singleton Factory Abstract Factory Prototype Summary 9/27
  • 10. 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
  • 11. SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Application of Factory design pattern 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 CONCEPT Design Goal : Reusability and Correctness Singleton Factory Abstract Factory Prototype Summary 11/27
  • 12. SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Application of Factory design pattern Client sendMessage() Customer getMessage() Frequent getMessage() Returning getMessage() Curious getMessage() Newbie getMessage() MailMessage text MailGenerationApplication getCustomerTypeFromUser() «setup» Factory: Email Generation Example Singleton Factory Abstract Factory Prototype Summary 12/27
  • 13. 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
  • 14. 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 Pattern Capture family creation in a class containing a factory method for each class in the family. * Gamma et al 7.4 Singleton Factory Abstract Factory Prototype Summary 14/27
  • 15. SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Abstract Factory* Abstract Factory Interface (Figure 7.17) Style…. Client StyleAFactory StyleBFactory Ensemble setAbstractFactory() doAFunction() AbstractFactory getAPart1Object() getAPart2Object() * relationships within pattern application not shown Singleton Factory Abstract Factory Prototype Summary 15/27
  • 16. SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Application of Abstract Factory Interface of Abstract Factory Applied to Word Processor Client SmallStyle LargeStyle StyleDocument setStyle() display() . . . . . . . 1 Singleton Factory Abstract Factory Prototype Summary 16/27
  • 17. SWE 316: Software Design and Architecture – Dr. Khalid Aljasser «create» The Abstract Factory Idea (Figure 7.19) AbstractFactory getAPart1Object() getAPart2Object() StyleAFactory getAPart1Object() getAPart2Object() Part1StyleA Part2StyleA Part1 Part2 abstractFactory 1 Ensemble setAbstractFactory() doAFunction() Client Singleton Factory Abstract Factory Prototype Summary 17/27
  • 18. SWE 316: Software Design and Architecture – Dr. Khalid Aljasser «create» Abstract Factory (Figure 7.20) Style…. AbstractFactory getAPart1Object() getAPart2Object() StyleAFactory StyleBFactory Part1StyleA Part1StyleB Part2StyleA Part2StyleB Part1 Part2 abstractFactory 1 Ensemble doAFunction() Client 1..n1..n Part… Singleton Factory Abstract Factory Prototype Summary 18/27
  • 19. 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 CONCEPT Design Goals : Correctness and Reusability Singleton Factory Abstract Factory Prototype Summary 19/27
  • 20. 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 CONCEPT Abstract Factory Design Pattern Singleton Factory Abstract Factory Prototype Summary 20/27
  • 21. 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 CONCEPT Prototype Pattern Singleton Factory Abstract Factory Prototype Summary 21/27
  • 22. SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Prototype Design Example: A Selection Graphics courtesy COREL Click on choice of storage: Click on choice of chair: Click on choice of desk:Furnit ure color Furnitu re hardwa re typecoloni al Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission. Singleton Factory Abstract Factory Prototype Summary 22/27
  • 23. 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 Factory Prototype Summary 23/27
  • 24. SWE 316: Software Design and Architecture – Dr. Khalid Aljasser The Prototype Idea Ensemble createEnsemble() Client MyPart clone(): MyPart MyPartStyleA clone() MyPartStyleB clone() myPartPrototype 1 // To create a MyPart instance: MyPart p = myPartPrototype.clone(); Singleton Factory Abstract Factory Prototype Summary 24/27
  • 25. SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Prototype Class Model Ensemble createEnsemble() Client Part1StyleA clone() Part1StyleB clone() Part2StyleA clone() Part2StyleB clone() Part1 clone() Part2 clone() part1Prototype part2Prototype 1 1 ..... // To create a Part1 object: Part1 p1 = part1Prototype.clone(); …. Part1StyleB returnObject = new Part1StyleB(); …. Part1StyleC clone() Singleton Factory Abstract Factory Prototype Summary 25/27
  • 26. 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 CONCEPT Design Goals : Correctness and Reusability Singleton Factory Abstract Factory Prototype Summary 26/27
  • 27. 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 Factory Prototype Summary 27/27

Editor's Notes

  • #2: In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.
  • #9: A class of which only a single instance can exist