Presentation is loading. Please wait.

Presentation is loading. Please wait.

SE 461 Software Patterns. FACTORY METHOD PATTERN.

Similar presentations


Presentation on theme: "SE 461 Software Patterns. FACTORY METHOD PATTERN."— Presentation transcript:

1 SE 461 Software Patterns

2 FACTORY METHOD PATTERN

3 When you see “new”, think “concrete”: Duck duck = new MallardDuck();

4 We have a banch of different duck classes and we don’t know until runtime which one we need to instantiate. Duck duck; if(picnic) { duck = new MalalrdDuck(); } else if(hunting) { duck = new DecoyDuck(); } …

5 What’s wrong with new: -Technically there’s nothing wrong with new. -Remember your code wil not be closed for modification if you do not code to an interface. -“identify the aspects that vary and seperate them from what stays the same”.

6 For flexibility, we realy want this to be an abstract class or interface, but we can not directly instantiate either of these. Pizza orderPizza() { Pizza pizza = new Pizza(); //abstract class or interface ! pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; }

7 You need more than one type of 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 The pressure is on to add more pizza types!

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(“clam”)) { pizza = new ClamPizza(); } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; }

10 Encapsulating object creation -Move object creation out of the orderPizza method -How?

11 Pizza orderPizza(String type) { Pizza pizza; //REMOVE THIS PART FROM orderPizza METHOD //ENCAPSULATE OBJECT CREATION pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; }

12 Encapsulated object creator: Public class SimplePizzaFactory { public Pizza createPizza(String type) { Pizza pizza = null; if(type.equals(“cheese”)) { pizza = new CheesePizza(); }else if(type.equals(“greek”)) { pizza = new GreekPizza(); } else if(type.equals(“pepperoni”)) { pizza = new PepperoniPizza(); } return pizza; }

13 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.cut(); pizza.box(); return pizza; } /other methods here

14

15 Franchising the pizza store -Everyone wants a PizzaStore in their own neighborhood. -You want a quality control. -But you also want flexibility for regional differences.

16

17 NYPizzaFactory nyFactory = new NYPizzaFactory(); PizzaStore nuStore = new PizzaStore(nyFactory); nyStore.order(“Veggie”); ChicagoPizzaFactory chicagoFactory = new ChicagoPizzaFactory(); PizzaStore chicagoStore = new PizzaStore(chicagoFactory); Chicago.order(“Veggie”);

18 A framework for the pizza store

19 PizzaStore is now an abstract class: public abstract class PizzaStore { public Pizza orderPizza(String type) { Pizza pizza; pizza = createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } abstract Pizza createPizza(String type); }

20 Allowing the subclasses to decide

21

22 orderPizza() is defined in the abstract PizzaStore, not the subclasses. So the method has no idea which subclass is actually running the code and making the pizza.

23 orderPizza() calls createPizza() to actually get a pizza. Which kind of pizza will it get? orderPizza() can not decide it.

24 When orderPizza() calls createPizza(), one of your subclasses will be called into to create a pizza.

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

26 public abstract class Pizza { String name, dough, sauce; ArrayList toppings = new ArrayList(); void prepare() { System.out.println(“Preparing “ + name); System.out.println(“Tossing dough…”); System.out.println(“Adding sauce…”); System.out.println(“Adding toppings:”); for(int i = 0;i<toppings.size();i++) System.out.println(“ “ + toppings.get(i)); } void bake() { System.out.println(“Bake for 25 minutes to 350”); } void cut() { System.out.println(“Cutting the pizza”); } void box() { System.out.println(“Place pizza in boxes”); } public String getName() { return name; }

27 public class NYStyleCheesePizza extends Pizza { public NYStyleCheesePizza() { name = “NY Style Sauce and Cheese Pizza”; dough = “Thin Crust Dough”; sauce = “Marinara Sauce”; toppings.add(“….”); }

28 ChicagoStyleCheesePizza is defined similarly.

29 public class PizzaTestDrive { public static void main(String[] args) { PizzaStore nyStore = new NYPizzaStore(); PizzaStore chicagoStore = new ChicagoPizzaStore(); Pizza pizza = nyStore.orderPizza(“cheese”); System.out.println(“Ethan ordered a “ + pizza.getName() + “\n”); pizza = chicagoStore.orderPizza(“cheese”); System.out.println(“Ethan ordered a “ + pizza.getName() + “\n”); }

30

31

32

33 The Factory Method Pattern: -Defines an interface for creating an object, -Lets subclasses decide which class to instantiate.

34 What about object dependencies?

35

36 The Dependency Inversion Principle: Depend upon abstraction. Do not depend upon concrete classes.

37 Applying the Principle Very Dependent PizzaStore: -it depends on every type of pizza After we apply the Factory Method: -We have an abstraction, Pizza abstract class. -High level (PizzaStore) and low level components (pizzas) depend on Pizza.

38

39 A few guidelines to help you follow the Principle: -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 its base class.

40 References


Download ppt "SE 461 Software Patterns. FACTORY METHOD PATTERN."

Similar presentations


Ads by Google