The Factory Pattern Sanjay Yadav (ISE2007009).

Slides:



Advertisements
Similar presentations
Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Advertisements

T O K ILL A S INGLETON F ACTORY M ETHOD P ATTERN Josh Mason 6/18/09.
Factory Pattern Building Complex Objects. New is an implementation  Calling “new” is certainly coding to an implementation  In fact, it’s always related.
Chapter 4: The Factory Pattern. Consider the Following Code Fragment Duck duck; if (picnic) { duck = new MallardDuck(); } else if (hunting) { duck = new.
CS 210 Introduction to Design Patterns September 19 th, 2006.
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
Informatics 122 Software Design II Lecture 5 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Brittany Johnson CSCI360 Abstract Factory Design Pattern.
Feb Ron McFadyen1 Factory Method Iterator Example : Java collection classes represent an example of the Factory Method design pattern. The.
Feb Ron McFadyen1 Adapter An adapter converts the interface of a class into another interface the client expects. An adapter lets classes work.
Factory Method By Judith Mziray And Jerry Cipolla.
March Ron McFadyen1 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly.
Creational Patterns: The Abstract Factory CSE 335 Spring 2008 E. Kraemer.
+ Informatics 122 Software Design II Lecture 8 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Factory Method Design Pattern (1) –A creational design.
Design Patterns.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
Beware of bugs in the above code; I have only proved it correct, not tried it.
Abstract Factory Abstract Factory using Factory Method.
12/6/20041 The Factory Method Pattern Presenters 王世賀 F 陳祐毓 F 張峻銘 F 吳佩達 F 林俊成 F 鄭榮智 F 許書豪 F
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Design Patterns Façade, Singleton, and Factory Methods Team Good Vibrations (1)
Mohammed Al-Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009 Factory Method Pattern.
By James Sheets. An object creational pattern. Separates the construction of a complex object from its representation so that the same construction process.
CS 210 Review Session October 5 th, Head First Design Patterns Chapter 4 Factory Pattern.
Factory Method Chris Colasuonno Also known as “Virtual Constructor”
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
CS 415 N-Tier Application Development By Umair Ashraf June 22,2013 National University of Computer and Emerging Sciences Lecture # 3 Design Patterns (Observer,Factory,Singleton)
Religious Studies 313 – Advanced Programming Topics.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Advanced Object-oriented Design Patterns Creational Design Patterns.
CSC 480 Software Engineering Design With Patterns.
Singleton Pattern Presented By:- Navaneet Kumar ise
Pat’s Pizza creation Pizza orderPizza(String type){ Pizza pizza; if (type.equals(“cheese”)) { pizza = new CheesePizza(); } else if type.equals(“greek”))
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
SE 461 Software Patterns. ABSTRACT FACTORY PATTERN.
SE 461 Software Patterns. FACTORY METHOD PATTERN.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Factory Method. Intent/Purpose Factory Method is used to deal with a problem of creating objects without specifying the EXACT class of object that we.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Factory Method Pattern. Admin SCPI Patner Day Feb. 21 Lunch count Presentation (4-8 min.) Practice on Feb. 16. Morning availablity on Feb21 Brief overview.
Design Patterns: MORE Examples
Factory Method Pattern
Design Pattern Catalogues
Low Budget Productions, LLC
Factory Patterns 1.
Software Design and Architecture
Creational Design Patterns
Factory Method Pattern
Programming Design Patterns
Software Engineering Lecture 7 - Design Patterns
Abstract Factory Pattern
CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”
CSC 480 Software Engineering
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Lesson 5: More on Creational Patterns
Creational Patterns.
CSC 480 Software Engineering
Software Design Lecture : 28.
Presentation transcript:

The Factory Pattern Sanjay Yadav (ISE2007009)

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.”

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.

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

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

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;

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

// 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..."); }

// 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..."); }

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

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;

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.

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.

Example 2: Frachising the pizza Store NYPizzaFactory PizzaStore ChicagoPizzaFactory

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()

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

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

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

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

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

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.

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.

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

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.

References: Head First Design Pattern Design Pattern by GOF http://en.wikipedia.org/wiki/Factory_method_pattern http://gsraj.tripod.com/design/creational/factory/factory.html

THANKYOU