Design Patterns Creational Patterns

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

29/03/14

PAGE 1
Creational Patterns www.elqoo.com

Part of the Design Patterns

www.elqoo.com

Object  cre
a1on  

PAGE 2

www.elqoo.com

www.elqoo.com

1  
29/03/14  

PAGE 3
Late  nigh

www.elqoo.com

www.elqoo.com

Crea%onal  Design  Pa/erns  


PAGE 4

www.elqoo.com

www.elqoo.com

2  
29/03/14  

Single  Object  
instance  

Content

PAGE 5
Creational Design Patterns

1 Singleton  

www.elqoo.com

Build  complex  
objects  

Content

PAGE 6
Creational Design Patterns

1 Singleton  

2 Builder  

www.elqoo.com

3  
29/03/14  

Create  Object  
Families  

Content

PAGE 7
Creational Design Patterns

1 Singleton  

2 Builder  

3 Abstract  Factory  

www.elqoo.com

Delegate  object  
crea1on  

Content

PAGE 8
Creational Design Patterns

1 Singleton  

2 Builder  

3 Abstract  Factory  

4 Factory  Method  

www.elqoo.com

4  
29/03/14  

Clone  objects  

Content

PAGE 9
Creational Design Patterns

1 Singleton  

2 Builder  

3 Abstract  Factory  

4 Factory  Method  

5 Prototype  
www.elqoo.com

PAGE 10
- SINGLETON -
Single object instances

www.elqoo.com

5  
29/03/14  

Problem Statement

PAGE 11
Ø  Hello  Brad  
SD   Ø  Hi  Suzy   PM  
Ø  We  would  like  to  manage  
preferences  in  different  
parts  of  our  applica1on  
Ø  I’ll  get  on  it  

Brad   Suzy  

www.elqoo.com

Problem Statement Overview

PAGE 12
Preferences need to be accessible from all over the application

Class  A   Class  G  

Class  B   Class  F  

Class  C   Class  D   Class  E  

www.elqoo.com

6  
29/03/14  

Problem Statement Detail

PAGE 13
Detailed explanation of the problem statement

•  Issues  
–  Mul1ple  classes  require  the  same  object  instance  
–  There  can  only  be  one  object  for  the  en1re  applica1on  
–  It  must  be  guaranteed  that  there  is  only  one  object  
•  Examples  
–  One  FileSystem,  Window  Manager  

www.elqoo.com

Single  Object  
instance  

Problem Solution

PAGE 14
Reason or logic behind the singleton pattern

•  Solu%on  1  
–  Create  a  global  variable  
à  Doesn’t  ensure  mul1ple  object  crea1on  
•  Solu%on  2  
–  Let  class  manage  its  one  instance  

Singleton  PaYern  

www.elqoo.com

7  
29/03/14  

Design Pattern Details

PAGE 15
{Code  Included}  

www.elqoo.com

Singleton Pattern

PAGE 16
Intent and known as

Intent  
Ensure  a  class  only  has  one  instance,  and  
provide  a  global  point  of  access  to  it.  

www.elqoo.com

8  
29/03/14  

Apply Singleton Pattern

PAGE 17
Applicability

•  When  
–  Only  one  instance  of  class  required  
–  Must  be  one  access  point    
–  Need  to  manage  object  instances  
 

One  instance   One  access  point  

www.elqoo.com

Singleton Pattern Structure

PAGE 18
Layout Structure of the singleton pattern

Store  Unique  Instance  


Singleton
static uniqueInstance
static getInstance()
createSingleton()
Only  way  to  retrieve  the  
instance  

Create  the  singleton  


instance  
www.elqoo.com

9  
29/03/14  

Singleton Pattern Consequences (1)

PAGE 19
•  Benefits  
–  Controlled  access  to  one  instance  
–  Reduce  name  space  à  Avoids  global  variables  
–  The  ability  to  subclass  the  singleton  class  

Preferences MyPreferences

www.elqoo.com

Singleton Pattern Consequences (2)

PAGE 20
•  Benefits  
–  Configure  the  number  of  instances  you  need  
•  Drawbacks  
–  State  of  the  singleton  must  be  shareable  between  
program  execu1ons  

www.elqoo.com

10  
29/03/14  

Conclusion

PAGE 21
•  Singleton  pa/ern  is  great  
–  Manage  number  of  instances  at  run1me  
–  Provide  single  unique  access  

www.elqoo.com

PAGE 22
- Builder -
Building complex objects

www.elqoo.com

11  
29/03/14  

Problem Statement

PAGE 23
Ø  Hello  Brad  
SD   Ø  Hi  Suzy   PM  
Ø  We  need  a  flexible  search  
screen  for  our  customer  
applica1on.    
Ø  I’ll  get  on  it.  

Brad   Suzy  

www.elqoo.com

Problem Statement Overview

PAGE 24
Designing a search screen that connects to multiple data sources

Mul1ple  Search  
Op1ons  
Three  different  
databases  

Data  is  spread  

www.elqoo.com

12  
29/03/14  

How would you implement the query?

PAGE 25
www.elqoo.com

Problem Statement Detail (1)

PAGE 26
UML representation of the search screen
Generic  Query  
Query   Interface  
combina1on  

Three  different  
databases  

www.elqoo.com

13  
29/03/14  

Problem Statement Detail (2)

PAGE 27
Copy of the construction logic

Construct  SQL  Query   SQLQuery  

Construct  MongoDb  Query   MongoDBQuery  

Construct  NoSql  Query   NoSqlQuery  

Similar  Code  
www.elqoo.com

Problem Statement Detail (3)

PAGE 28
Detailed explanation of the screen design problems

•  Issues  
–  Search  screen  results  in  three  objects  
•  One  for  each  database  
–  Need  to  construct  the  three  objects  over  and  over  again  
•  Future  proof  
–  Ideally  we  would  like  to  support  a  fourth  database  
without  changing  too  much  code  

www.elqoo.com

14  
29/03/14  

Problem Solution

PAGE 29
Interface  that  all  
Create a builder to centralize the building process queries  must  
comply  to  

Return  result  
Centrally  build   Mul1ple  
the  queries   defin1ons  

www.elqoo.com

Design Pattern Details

PAGE 30

www.elqoo.com

15  
29/03/14  

Builder Pattern

PAGE 31
Intent and known as

Intent  
Separate  the  construc%on  of  a  complex  
object  from  its  representa%on  so  that  the  
same  construc%on  process  can  create  
different  representa%ons.  

www.elqoo.com

Apply Builder Pattern

PAGE 32
Applicability

•  Use  
–  Separate  construc%on  with  internal  representa1on  
–  One  process  à  mul%ple  object  representa%on  
–  Object  construc1on  <>  object  assembling  

New  Object   New  Object  

Set  part  A,  set  part  B  

www.elqoo.com

16  
29/03/14  

Generic  
Builder Pattern Structure

PAGE 33
interface  to  
Layout structure of the builder pattern build  complex  
objects  

Builds  concrete  
Uses  builder(s)   product  
to  construct  an  
object  

Specific  
implementa1on  
Need  a  result  
www.elqoo.com

Builder Pattern Collaboration

PAGE 34
How objects work with a builder

3
4

5 www.elqoo.com

17  
29/03/14  

Builder Pattern Consequences

PAGE 35
Benefits of the builder pattern

•  Benefits  
–  Uniform  produc%on  crea1on  via  an  interface  
–  Abstract  building  process  
–  Loose  coupling  
•  Construc1on  
•  Representa1on  
–  Finer  control  on  the  build  process  à  Allow  mul1ple  steps  

www.elqoo.com

Conclusion

PAGE 36
•  Builder  pa/ern  is  great  
–  One  build  process  for  mul1ple  similar  objects  
–  Advance  control  over  the  build  process  

www.elqoo.com

18  
29/03/14  

PAGE 37
- Abstract Factory-
Create Object Families

www.elqoo.com

Problem Statement

PAGE 38
Ø  Hello  Brad  
SD   Ø  Hi  Suzy   PM  
Ø  We  want  to  use  different  
themes  in  our  applica1on  
Ø  No  problem,  pink  too?  

Brad   Suzy  

www.elqoo.com

19  
29/03/14  

Problem Statement Overview

PAGE 39
•  Requirement  
–  Easy  change  theme  
–  Add  new  themes  
•  Avoid  
–  Hard  coded  themes  

www.elqoo.com

Client  works  
with  abstract  
Problem Statement Detail

PAGE 40
UML representation of a theme design
classes  
One  
implementa1on  
per  theme    

How  will  the  client   Classes  belong  


create  the  right   together  
classes?  

Framework  
www.elqoo.com

20  
29/03/14  

Introduce  a  widget  
Problem Solution factory  

PAGE 41
Use abstract classes and factories

Create  Pink  
Scrollbar  
+Window  

Concrete   Client  only  works  


implementa1ons   with  an  abstrac1on  
www.elqoo.com

Design Pattern Details

PAGE 42

www.elqoo.com

21  
29/03/14  

Abstract Factory Pattern

PAGE 43
Intent and known as

Intent  
Provide  an  interface  for  crea%ng  families  of  
related  or  dependent  objects  without  
specifying  their  concrete  classes.  

Known  As  
Kit  
www.elqoo.com

Apply Abstract Factory Pattern

PAGE 44
Applicability

•  Use  
–  Crea%on  of  products  independent  from  the  applica%on  
–  Configura1on  of  product  families  is  required  
–  Hide  product  implementa1on  à  only  provide  interface  

www.elqoo.com

22  
29/03/14  

Product  
Abstract Factory Pattern Structure

PAGE 45
familie  
Layout structure of the abstract factory pattern

Introduce  
a  widget  
factory  

Concrete  
Client  only  works  
implementa1ons  
with  an  abstrac1on  
www.elqoo.com

Abstract Factory Pattern Consequences

PAGE 46
Considerations for the abstract factory pattern

•  Benefits  
–  Control  the  classes  of  object  to  be  created  
–  Exchanging  product  families  easy  
–  Promote  consistency  among  products  
•  Drawbacks  
–  Addi%on  of  new  products  is  difficult  à  extend  factory  
interface  
www.elqoo.com

23  
29/03/14  

Conclusion

PAGE 47
•  Abstract  Factory  pa/ern  is  great  
–  Crea%ng  product  families  
–  Centralize  crea1on  logic  

www.elqoo.com

PAGE 48
- Factory Method-
Delegate object creation

www.elqoo.com

24  
29/03/14  

Problem Statement

PAGE 49
Ø  Hello  Brad  
SD   Ø  Hi  Suzy   PM  
Ø  Can  you  help  our  
developers  only  focus  on  
the  new  func1onality  to  
implement  for  our  
document  framework?  
Brad   Ø  No  problem   Suzy  

www.elqoo.com

PAGE 50
Document  Framework  

www.elqoo.com

25  
29/03/14  

Problem Statement Overview

PAGE 51
Required functionality for the document framework

•  Framework  Requirement  
–  Flexible  create  new  documents  
–  Document  type  per  applica%on  
–  Re-­‐use  document  code    

www.elqoo.com

Problem Statement Detail

PAGE 52
What  type  of  object  
UML representation of the document framework will  be  created?  

Document  Framework  
Extend  the  Document  

DocumentA

Applica1on  A   Applica1on  B  
www.elqoo.com

26  
29/03/14  

Problem Solution

PAGE 53
Generic  Applica1on  
Delegate creation of documents
Override  create   Class  
method  

Extend  
Applica1on  class  

Work  with  
MyDocument  
www.elqoo.com
implementa1on  

Design Pattern Details

PAGE 54

{Code  Included}  

www.elqoo.com

27  
29/03/14  

Factory Method Pattern

PAGE 55
Intent and known as

Intent  
Define  an  interface  for  crea%ng  an  object  but  
let  subclasses  decide  which  class  to  
instan1ate.  Factory  Method  lets  a  class  defer  
instan%a%on  to  subclasses.  
Known  As  
Virtual  Constructor  
www.elqoo.com

Apply Factory Method Pattern

PAGE 56
Applicability

•  When  
–  Class  can’t  expect  the  type  of  object  it  must  create  
–  Subclasses  must  decide  what  types  of  objects  are  created  
 

www.elqoo.com

28  
29/03/14  

Creator  defines  a  
Factory Method Pattern Structure

PAGE 57
factory  method  
Layout structure of the factory method pattern

Override  factory  
Is  implemented  by  a  
method  à  create  
subclass  
desired  product  
www.elqoo.com

Factory Method Pattern Consequences

PAGE 58
Considerations for the factory method pattern

•  Benefits  
–  Delegate  object  crea1on  
–  Hooks  for  subclasses  
–  Base  class  can  provide  a  default  implementa%on  

www.elqoo.com

29  
29/03/14  

Conclusion

PAGE 59
•  Factory  Method  pa/ern  is  great  
–  Delegate  object  crea%on  at  run1me  
–  Don’t  know  what  type  of  class  you  need  to  create  

www.elqoo.com

PAGE 60
- Prototype-
Create object on
prototype

www.elqoo.com

30  
29/03/14  

Problem Statement

PAGE 61
Ø  Hello  Brad  
SD   Ø  Hi  Suzy   PM  
Ø  Our  Graphic  framework  
isn’t  as  usable  as  we  
thought.    
Ø  Strange,  I  thought  it  was  
well  designed  
Brad   Suzy  

www.elqoo.com

Base  Graphic  Object  


Problem Statement Overview

PAGE 62
UML representation of the graphics framework

Framework   Two  Types  

Needs  to  ini1alize  a  


new  Graphic,  but  
cannot  say  new  
“GraphicClass”  
Applica1on  Specific  
www.elqoo.com

31  
29/03/14  

Ini1alize  GraphicTool   Support  the  clone  


Problem Solution

PAGE 63
with  Graphic  Prototype   interface  
Prototyping the graphics class

Use  clone()  for  


crea1on  new  objects  

www.elqoo.com

Design Pattern Details

PAGE 64

www.elqoo.com

32  
29/03/14  

Prototype Pattern

PAGE 65
Intent and known as

Intent  
Specify  the  kinds  of  objects  to  create  using  a  
prototypical  instance,  and  create  new  objects  
by  copying  this  prototype.  

www.elqoo.com

Apply Prototype Pattern

PAGE 66
Applicability

•  When  
–  Classes  to  instan1ate  are  specific  at  run-­‐%me  
–  Avoid  building  class  hierarchies  (abstract  factory  paYern)  
–  A  class  can  have  limited  instances  of  state  
•  Cloning  is  more  efficient  

www.elqoo.com

33  
29/03/14  

All  prototypes  
support  the  clone  
Prototype Pattern Structure

PAGE 67
Layout structure of the prototype pattern
method  

Client  is  wired  with  a  


prototype  at  startup  

New  object  via  clone  


method  

www.elqoo.com

Prototype Pattern Consequences

PAGE 68
Considerations for the prototype pattern

•  Benefits  
–  Add/remove  products  at  run%me  
–  Reduce  sub  classing  (avoid  abstract  factory)  
–  Configure  applica1on  dynamically  
•  Drawbacks  
–  Requires  to  create  prototypes  before  other  object  crea1on  

www.elqoo.com

34  
29/03/14  

Conclusion

PAGE 69
•  Prototype  pa/ern  is  great  
–  Configure  object  crea%on  by  cloning  
–  Dynamically  change  object  crea%on  

www.elqoo.com

35  

You might also like