the simple factory pattern and factory method patternjin/3132/lectures/dp-07.pdf · the simple...

33
The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Upload: others

Post on 21-May-2020

17 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

The Simple Factory Pattern and

Factory Method Pattern

CSCI 3132 Summer 2011 1

Page 2: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

New  is  an  implementa-on  

•  Calling  “new”  is  certainly  coding  to  an  implementa-on  

•  In  fact,  it’s  always  related  to  a  concrete  class  •  (Open  for  Modifica-on)  

•  That’s  fine  when  things  are  simple,  but  .  .  .  

2

Page 3: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Look  Out  For  Change  

•  When  you  have  several  related  classes,  that’s  probably  a  good  sign  that  they  might  change  in  the  future  

Duck duck;!if (picnic) {!! duck = new MallardDuck();!} else if (hunting) {!! duck = new DecoyDuck();!} else if (inBathTub) {!! duck = new RubberDucky();!}!

3

Page 4: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

   Instan-a-ng  Concrete  Classes  

•  Using  new  instan-ates  a  concrete  class.  •  This  is  programming  to  implementa-on  instead  of  interface.  

•  Concrete  classes  are  oKen  instan-ated  in  more  than  one  place.  

•  Thus,  when  changes  or  extensions  are  made  all  the  instan-a-ons  will  have  to  be  changed.  

•  Such  extensions  can  result  in  updates  being  more  difficult  and  error-­‐prone.  

4

Page 5: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Example  

Suppose  that  you  are  required  to  develop  a  

system  that  accepts  orders  for  pizzas.    There  

are  three  types  of  pizzas,  namely,  cheese,  

Greek,  and  pepperoni.  The  pizzas  differ  

according  to  the  dough  used,  the  sauce  used  

and  the  toppings.  

Draw  a  class  diagram  for  the  system.  

5

Page 6: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Design  Principle  • What  should  you  try  to  do  with  code  that  changes?  

Pizza orderPizza() {!! Pizza pizza = new Pizza(); !

! pizza.prepare();!! pizza.bake();!! pizza.cut();!! pizza.box();!

! return pizza;!}!

6

Page 7: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Pizza orderPizza(String type) {!! Pizza pizza;!

! if (type.equals(“cheese”)) {!! !pizza = new CheesePizza();!! } else if (type.equals(“greek”)) {!! !pizza = new GreekPizza();!! } else if (type.equals(“pepperoni”)) {!! !pizza = new PepperoniPizza();!! }!

! pizza.prepare();!! pizza.bake();!! pizza.cut();!! pizza.box();!

! return pizza;!}! 7

Page 8: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Example:Class  Diagram  

Problems  with  the  design  8

Page 9: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Pizza orderPizza(String type) {!! Pizza pizza;!

! if (type.equals(“cheese”)) {!! !pizza = new CheesePizza();!! } else if (type.equals(“greek”)) {!! !pizza = new GreekPizza();!! } else if (type.equals(“pepperoni”)) {!! !pizza = new PepperoniPizza();!! } else if (type.equals(“sausage”)) {!! !pizza = new SausagePizza();!! } else if (type.equals(“veggie”)) {!! !pizza = new VeggiePizza();!! }!

! pizza.prepare();!! pizza.bake();!! pizza.cut();!! pizza.box();!! return pizza;!}!

9

Page 10: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Pizza orderPizza(String type) {!! Pizza pizza;!

! if (type.equals(“cheese”)) {!! !pizza = new CheesePizza();!! } else if (type.equals(“greek”)) {!! !pizza = new GreekPizza();!! } else if (type.equals(“pepperoni”)) {!! !pizza = new PepperoniPizza();!! } else if (type.equals(“sausage”)) {!! !pizza = new SausagePizza();!! } else if (type.equals(“veggie”)) {!! !pizza = new VeggiePizza();!! }!

! pizza.prepare();!! pizza.bake();!! pizza.cut();!! pizza.box();!! return pizza;!}!

Encapsulate!

10

Page 11: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Revised  System  

•  Suppose  that  the  Greek  pizza  is  not  popular  and  must  be  removed  and  two    new  pizzas,  Clam  and  Veggie,  must  be  added  to  the  menu.  

•  Programming  to  implementa-on  like  makes  such  changes  difficult.  

•  Crea-ng  a  SimpleFactory  to  encapsulate  the  code  that  changes  will  make  the  design  more  flexible.  

•  Remove  the  code  that  creates  a  pizza  –  forms  a  factory.  

11

Page 12: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

public class SimplePizzaFactory {!! public Pizza createPizza(String type) {!! ! Pizza pizza;!

! ! if (type.equals(“cheese”)) {!! ! ! pizza = new CheesePizza();!! ! } else if (type.equals(“pepperoni”)) {!! ! ! pizza = new PepperoniPizza();!! ! } else if (type.equals(“sausage”)) {!! ! ! pizza = new SausagePizza();!! ! } else if (type.equals(“veggie”)) {!! ! ! pizza = new VeggiePizza();!! ! }!

! ! return pizza;!! }!}!

Now orderPizza() is tidy

12

Page 13: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

public class PizzaStore {!! SimplePizzaFactory factory;!

! public PizzaStore(SimplePizzaFactory factory) {!! ! this.factory = factory;!! }!

! public Pizza orderPizza(String type) {!! ! Pizza pizza;!! ! pizza = factory.createPizza(type);!

! ! pizza.prepare();!! ! pizza.bake();!! ! pizza.cute();!! ! pizza.box();!

! ! return pizza;!! }!}!

No new operator

No new() operator

13

Page 14: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

   Example:  Revised  Class  Diagram    

14

Page 15: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Simple  Factory  

•  Pull  the  code  that  builds  the  instances  out  and  put  it  into  a  separate  class    – Iden-fy  the  aspects  of  your  applica-on  that  vary  and  separate  them  from  what  stays  the  same  

15

Page 16: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Simple  Factory  Pa]ern  

Client orderProduct()

SimpleFactory createProduct()

<<abstract>> Product

productMethod()

ConcreteProductB

ConcreteProductA

ConcreteProductC

16

Page 17: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Why  would  we  do  this?  

• Mul-ple  clients    needing  same  types  of  object  •  Ensure  consistent  object  ini-aliza-on  

17

Page 18: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

   Example:  System  Revision  Again  

Franchises  in  different  parts  of  the  country  are    now  adding  their  own  special  touches  to  the    pizza.    For  example,  customers  at  the  franchise  in  New  York  like  a  thin  base,  with  tasty  sauce    and  li]le  cheese.    However,  customers  in    Chicago  prefer  a  thick  base,  rich  sauce  and  a    lot  of  cheese.    Some  franchises  also  cut  the    pizza  slices  differently  (e.g.  square)  

You  need  to  extend  the  system  to  cater  for  this.  

18

Page 19: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

The  Factory  Method  

Defini-on:    •         The  factory  Method  Pa]ern  defines  an  interface  for  crea-ng  an  object,  but  lets  the  subclasses  decide  which  class  to  instan-ate.  

•         Factory  Method  lets  a  class  defer  instan-a-on  to  subclasses.  

19

Page 20: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Creator  Class  

20

Page 21: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Product  Class  

21

Page 22: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

public abstract class PizzaStore {!! !! public Pizza orderPizza(String type) {!! ! Pizza pizza;!! ! pizza = createPizza(type);!

! ! pizza.prepare();!! ! pizza.bake();!! ! pizza.cute();!! ! pizza.box();!

! ! return pizza;!! }! abstract Pizza createPizza(String type);!}!

No new operator

22

Page 23: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

public class NYPizzaStore extends PizzaStore{! Pizza createPizza(String item) {!! !! if (type.equals(“cheese”)) {!! !! ! return new NYStyleCheesePizza();!! !! } else if (type.equals(“pepperoni”)) {!! !! ! return new NYStylePepperoniPizza();!! !! } else if (type.equals(“veggie”)) {!! !! ! return new NYStyleVeggiePizza();!! !! } else return null;! }!}!

23

Page 24: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Structure  

24

Page 25: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Factory  Method  Pa]ern  

<<abstract>> Creator

factoryMethod() operation()

<<abstract>> Product

ConcreteCreator factoryMethod()

ConcreteProduct

abstract!

IS-A

HAS-A

IS-A

No more SimpleFactory class Object creation is back in our class, but … delegated to concrete classes

25

Page 26: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Applicability  

•  Use  Factory  Method  when:  – A  class  can’t  an-cipate  the  class  of  objects  it  must  create  

– A  class  wants  its  subclasses  to  specify  the  objects  it  creates  

– Classes  delegate  to  one  of  several  helper  subclasses,  and  you  want  to  localize  knowledge  about  which  is  the  delegate  

26

Page 27: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Par-cipants  

•  Product  – Defines  interface  of  objects  to  be  created  

•  ConcreteProduct  – Implements  Product  interface  

•  Creator  – Declares  factory  method  

• Returns  object  of  type  Product  • May  define  default  implementa-on  

– May  call  factory  method  to  create  Products  

27

Page 28: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Consequences  

•  Eliminates  need  to  bind  crea-on  code  to  specific  subclasses  

• May  need  to  subclass  Creator  for  each  ConcreteProduct  

•  Provides  hooks  for  subclasses  •  Connects  parallel  class  hierarchies  

28

Page 29: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

An  Example  

•  Applica-on  framework  – App  subclasses  “Applica-on,”  “Document”  

– Framework  knows  when  to  create  Document,  but  Doesn’t  know  what  type  of  Document  to  create  

– Factory  Method  moves  knowledge  about  specific  Document  subclass  out  of  framework  

29

Page 30: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Guidelines  

•  No  variable  should  hold  a  reference  to  a  concrete  class  

•  No  class  should  derive  from  a  concrete  class  

•  No  method  should  override  an  implemented  method  of  an  of  its  base  classes.  

30

Page 31: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Summary  

•  The  creator  gives  you  an  interface  for  wri-ng  objects.  

•  The  creator  specifies  the  “factory  method”,  e.g.  the  createPizza  method.  

•  Other  methods  generally  included  in  the  creator  are  methods  that  operate  on  the  product  produced  by  the  creator,  e.g.  orderPizza.  

•  Only  subclasses  implement  the  factory  method.  

31

Page 32: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Summary  

•   Simple  Factory  – Use  when  you  have  only  one  family  of  objects  

•  Factory  Method  Pa]ern  – Use  when  you  have  mul-ple  families  of  objects  

•  Abstract  Factory  (next  -me)  – Use  when  you  have  mul-ple  families  of  object  components  

32

Page 33: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1

Thoughts…  

• When  you  have  code  that  instan-ates  concrete  classes  that  may  change,  encapsulate  the  code  that  changes.  

•  The  factory  pa]ern  allows  you  to  encapsulate  the  behaviour  of  the  instan-a-on.  

•  Duplica-on  of  code  is  prevented.  •  Client  code  is  decoupled  from  actual  implementa-ons.  

•  Programming  to  interface  not  implementa-on.  

33