Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Factory Pattern Sanjay Yadav (ISE2007009).

Similar presentations


Presentation on theme: "The Factory Pattern Sanjay Yadav (ISE2007009)."— Presentation transcript:

1 The Factory Pattern Sanjay Yadav (ISE )

2 Factory Design Pattern
The factory pattern is creational software design pattern. Its intention is to “Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method pattern lets a class defer instantiation to subclasses.”

3 Also Known As: Steps: Virtual Constructor
Identify the aspects that vary. Encapsulating object creation. Place that code in a object from that is only worry about how to create objects.

4 Class Diagram: An example
Pizza PizzaStore SimplePizzaFactory orderPizza() CreatePizza() Prepare() Bake() Cut() Box() PepperoniPizza CheesePizza VeggiePizza ClamPizza

5 Implementation public class PizzaDemo {
public static void main(String[] args) // first we create PizzaStore PizzaStore store = new PizzaStore( new SimplePizzaFactory()); store.orderPizza("cheese"); // then use store to make order store.orderPizza("veggie"); }

6 public class PizzaStore //Give PizzaStore a reference to a SimplePizzaFacotry
{ SimplePizzaFactory factory; public PizzaStore(SimplePizzaFactory factory){ this.factory = factory; } public Pizza orderPizza(String type){ // PizzaStore gets the factory passed to it in Pizza pizza; // the constructor pizza = factory.createPizza(type); pizza.prepare(); //orderPizza() method uses the factory to create its Pizzas pizza.bake(); // by simply passing on the type of order. pizza.cut(); pizza.box(); return pizza;

7 //SimplePizzaFactory for creating pizzas for its clients.
public class SimplePizzaFactory { public Pizza createPizza(String type){ // createPizza() method in Pizza pizza = null; // the factory. This is the // method all clients will if(type.equals("cheese")){ // use to instantiate new pizza = new CheesePizza(); // objects. }else if(type.equals("veggie")){ pizza = new VeggiePizza(); } return pizza;

8 // one concrete subclass how about defining Cheese style Pizza
public class CheesePizza extends Pizza { public CheesePizza(){ name = "Cheese Pizza"; dough= "Thin crust dough"; sauce = "Tomato sauce"; toppings.add("Grated Cheese..."); }

9 // one concrete subclass how about defining Veggie style Pizza
public class VeggiePizza extends Pizza { public VeggiePizza(){ name = "Veggie Pizza"; dough= "Thick crust dough"; sauce = "Tomato sauce"; toppings.add("Grated Veggie..."); }

10 // Its an abstract Pizza & all concrete Pizzas will derive from this
import java.util.*; public abstract class Pizza { String name; // each Pizza has a name, a type of dough, a type String dough; // of sauce & set of toppings. String sauce; ArrayList toppings = new ArrayList(); void prepare(){ System.out.println("Preparing"+ name); // The abstract class provides some basic System.out.println("Tossing dough..."); // defaults for baking, cutting & boxing System.out.println("Adding sauce..."); System.out.println("Adding toppings:"); for(int i =0; i< toppings.size(); i++){ System.out.println(" "+toppings.get(i)); }

11 void bake(){ System.out.println("Bake for 25 minutes"); } void cut(){ System.out.println("Cutting pizza"); void box(){ System.out.println("Place the pizza in box"); String getName(){ return name;

12 Motivation: Frameworks use an abstract class to define and maintain relationship between objects. A framework is often responsible for creating these objects as well. Factory pattern encapsulates the knowledge of which Pizza subclass to create and moves this knowledge out of the framework.

13 Applicability: Use of Factory Method pattern when
When a class does not know which class of objects it must create A class specifies its sub-classes to specify which objects to create. In programmer’s language (very raw form), you can use factory pattern where you have to create an object of any one of sub-classes depending on the data provided.

14 Example 2: Frachising the pizza Store NYPizzaFactory PizzaStore
ChicagoPizzaFactory

15 Allowing the subclasses to decide:
Each subclass overrides createpizza() method, while all subclasses use orderpizza() method defined in PizzaStore. CreatePizza() is abstract in PizzaStore. So all PizzaStore subtype must implement the method. PizzaStore Createpizza() Orderpizza() ChicagoStylePizzaStore NYStylePizzastore Createpizza() Createpizza()

16 A framework for the pizza store
public abstract class PizzaStore{ public Pizza orderPizza(String type){ Pizza pizza; pizza = createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } protected abstract Pizza createPizza(String type);

17 Let’s make a NYPizzaStore
public class NYPizzaStore extends PizzaStore { public Pizza createPizza(String item) { if (item.equals("cheese")){ return new NYstyleCheesePizza(); }else return null; }

18 Concrete subclass- NYStyleCheesePizza
public class NYstyleCheesePizza extends Pizza { public NYstyleCheesePizza() { name= " NY style Sauce and Cheese Pizza"; dough= "Thin Crust Dough"; sauce= "Marinara Sauce"; toppings.add("Grated Reggiano Cheese"); }

19 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("Joel ordered a " + pizza.getName() + "\n"); }

20 Structure: Creator Product ConcreteCreator ConcreteProduct
factoryMethod() anOperation() Product ConcreteCreator ConcreteProduct factoryMethod

21 Participants: Creator(PizzaStore): ConcreteCreator ConcreteProduct:
1)The creator is a class that contains the implementations for all of the methods to manipulate products, except for factory method 2)The abstract factoryMethod() is what all creator subclasses must implement. ConcreteCreator The ConcreteCreator implements the factoryMethod(), which is the method that actually produces products. ConcreteProduct: The ConcreteCreator is responsible for creating one or more concrete products. It is only class that has the knowledge of how to create these products. Product(Pizza): All products must implement the same interface so that the classes which use the products can refer to the interface not the concrete class.

22 Collaborations: Creator relies on its subclasses to define the factory method so that it returns an instance of the appropriate ConcreteProduct. Consequences: Factory methods eliminate the need to bind application-specific classes into your code only deals with the Product interface; therefore it can work with any user defined ConcreteProduct classes Disadvantage of factory method is that clients might have to subclass the Creator class just to create a particular ConcreteProduct object.

23 Related Patterns: Abstract Factory – often implemented using factory methods. Builder pattern Template method pattern – may call factory methods.

24 Known Uses: In MacApp and ET++ [WGM88].
In the Smalltalk-80 Model/View/Controller framework. The Orbix ORB system from IONA Technologies [ION94] uses Factory Method.

25 References: Head First Design Pattern Design Pattern by GOF

26 THANKYOU


Download ppt "The Factory Pattern Sanjay Yadav (ISE2007009)."

Similar presentations


Ads by Google