Presentation is loading. Please wait.

Presentation is loading. Please wait.

Factory Pattern Building Complex Objects. New is an implementation  Calling “new” is certainly coding to an implementation  In fact, it’s always related.

Similar presentations


Presentation on theme: "Factory Pattern Building Complex Objects. New is an implementation  Calling “new” is certainly coding to an implementation  In fact, it’s always related."— Presentation transcript:

1 Factory Pattern Building Complex Objects

2 New is an implementation  Calling “new” is certainly coding to an implementation  In fact, it’s always related to a concrete class  (Open for Modification)  That’s fine when things are simple, but...  Calling “new” is certainly coding to an implementation  In fact, it’s always related to a concrete class  (Open for Modification)  That’s fine when things are simple, but...

3 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(); }  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(); }

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

5 When Building Objects is Complex  Book example - a variety of types of pizzas  Think about constructing the weapons in your decorator lab  Book example - a variety of types of pizzas  Think about constructing the weapons in your decorator lab

6 Pizza orderPizza() { Pizza pizza = new Pizza(); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } Pizza orderPizza() { Pizza pizza = new Pizza(); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; }

7 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; } 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; }

8 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; } 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 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; } 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 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; } 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

11 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; } 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

12 Simple Factory  Pull the code that builds the instances out and put it into a separate class  Identify the aspects of your application that vary and separate them from what stays the same  Pull the code that builds the instances out and put it into a separate class  Identify the aspects of your application that vary and separate them from what stays the same

13 Simple Factory Pattern Client orderProduct() SimpleFactory createProduct() > Product productMethod() ConcreteProductB ConcreteProductA ConcreteProductC

14 Why would we do this?  Multiple clients needing same types of object  Ensure consistent object initialization  Multiple clients needing same types of object  Ensure consistent object initialization

15 The Factory Method Definition: The factory Method Pattern defines an interface for creating an object, but lets the subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. Definition: The factory Method Pattern defines an interface for creating an object, but lets the subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

16 Factory Method Pattern > Creator factoryMethod() operation() > 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

17 Dependency Inversion Principle Depend upon abstractions -- not concrete classes Depend upon abstractions -- not concrete classes

18 Impossible 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.  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.

19 Impossible Guidelines The idea here is to stay clear of classes that are likely to change String studentChecking SystembusinessChecking IntegerFederalTaxCode The idea here is to stay clear of classes that are likely to change String studentChecking SystembusinessChecking IntegerFederalTaxCode

20 What if there are categories of Products? > Player HeroGunner HeroStealth HeroTank HeroPilot EnemyLevelBoss EnemyCaptain EnemyThug

21 When Subclasses get out of hand… make the factory an abstract class as well Instead of subclasses for the products, subclasses for the components of the products The next diagram is overwhelming… breathe in, breath out

22 Abstract Factory Pattern > AbstractFactory createProductA() createProductB() > AbstractProductA > AbstractProductB Client ConcreteFactory1 createProductA() createProductB() ConcreteFactory2 createProductA() createProductB() ConcreteProductA1 ConcreteProductA2 ConcreteProductB1 ConcreteProductB2

23 Abstract Factory Pattern This design favors composition handy when you need to insure the quality of many similar, but slightly different versions of a class

24 More Diagrams  pp.156/157  160/161  pp.156/157  160/161

25 Summary  Simple Factory  Use when you have only one family of objects  Factory Method Pattern  Use when you have multiple families of objects  Abstract Factory  Use when you have multiple families of object components  Simple Factory  Use when you have only one family of objects  Factory Method Pattern  Use when you have multiple families of objects  Abstract Factory  Use when you have multiple families of object components


Download ppt "Factory Pattern Building Complex Objects. New is an implementation  Calling “new” is certainly coding to an implementation  In fact, it’s always related."

Similar presentations


Ads by Google