Factory Method Pattern

Slides:



Advertisements
Similar presentations
Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable.
Advertisements

T O K ILL A S INGLETON F ACTORY M ETHOD P ATTERN Josh Mason 6/18/09.
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
Factory Method By Judith Mziray And Jerry Cipolla.
Creational Patterns: The Abstract Factory CSE 335 Spring 2008 E. Kraemer.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Design Patterns.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
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.
Mohammed Al-Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009 Factory Method 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.
Abstract Factory and Factory Method CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
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.
DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem.
Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation.
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.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
The Factory Pattern Sanjay Yadav (ISE ).
Advanced Object-oriented Design Patterns Creational Design Patterns.
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.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Abstract Factory pattern Intent Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
SOFTWARE DESIGN Design Patterns 1 6/14/2016Computer Science Department, TUC-N.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
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.
Patterns in programming
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Presented by FACADE PATTERN
Design Patterns: MORE Examples
Abstract Factory Pattern
Factory Method Pattern
Strategy Design Pattern
Design Pattern Catalogues
Low Budget Productions, LLC
Factory Patterns 1.
Creational Pattern: Prototype
Software Design and Architecture
Flyweight Design Pattern
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Presented by Igor Ivković
Object Oriented Programming
Design Patterns - A few examples
Object Oriented Design Patterns - Creational Patterns
Generation Gap By Kurt Rehwinkel
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.
Presented by Igor Ivković
Adapter Pattern Jim Fawcett
Software Design Lecture 9.
Iterator Design Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014
Adapter Pattern Jim Fawcett
Presentation transcript:

Factory Method Pattern CSE776-Design Patterns Jim Fawcett Factory Method Pattern

Intent “Define an interface for creating an object, but let subclasses decide which class to instantiate” It lets a class defer instantiation to subclasses at run time. It refers to the newly created object through a common interface.

Also Known as Virtual Constructor The main intent of the virtual constructor idiom in C++ is to create a copy of an object or a new object without knowing its concrete type and this is exactly what the Factory Method does.

Motivation Frameworks: Factory Method is used in frameworks where library code needs to create objects of types which may be sub classed by applications using the framework. Since the library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application, it can use the Factory Method.

Motivating Examples – Cont.

Forces We want to have a set of reusable classes which are flexible enough to be extended. The client does not know the type of object that needs to be created in advance and still wants to perform operations on them.

Applicability Factory Method is needed when: A class can’t anticipate the types of objects it must create. A class wants its subclasses to specify the object to create. The designer wants to localize knowledge of helper sub classes.

Basic Structure

Participants Product (IHttpHandler) Defines the interface of objects the factory method creates. ConcreteProduct (ASP.SamplePage_aspx) Implements the Product Interface Creator (IHttpHandlerFactory) Declares the factory method and may provide a default implementation for it. Defines the return type as Product. ConcreteCreator (PageHandlerFactory) Overrides the factory method to return an instance of ConcreteProduct.

Collaborators The Creator relies on the subclass’s factory method to return an instance of appropriate ConcreteProduct object. The Creator executes some sequence of operations on the object or simply returns a reference to Product (bound to the ConcreteProduct object) to the client.

Consequences The client code deals only with the product interface, therefore it can work with any user defined Concrete Product classes (decoupling subclass details from client classes). New concrete classes can be added without recompiling the existing client code. It may lead to many subclasses if the product objects requires one or more additional objects. (Parallel class hierarchy)

Consequences – Cont.

Implementation Two major varieties Abstract Creator class with no default implementation Concrete Creator with default implementation. Other variations: Parameterized Methods Templates

Parameterized Factory Methods class Creator { public: virtual Product* Create(ProductID id) { if (id == P1) return new MyProduct; if (id == P2) return new YourProduct; // other products ... return 0; } }; // You can subclass the Creator to handle more IDs Product* MyCreator::Create(ProductID id) { if (id == P3) return new TheirProduct; // Handle other IDs return this->Creator::Create(id);

Templatized Factory Methods class Creator { public: Creator() { // You won’t call factory method here (why?) // Use lazy initialization instead } virtual Product* CreateProduct() = 0; }; template <class T> class StandardCreator: public Creator { virtual Product* CreateProduct() { return new T; // In the Client StandardCreator<MyProduct> myCreator;

Known Uses It is a pervasive pattern. It is used in several places in the Java API. For example, URLConnection has a method getContent that returns the content as an appropriate object (html, gif etc.) .Net Framework Class Library Factory method is used in: Systems.Collections.IEnumerable, System.Net.WebRequest System.Security.Cryptography

Related Patterns Abstract Factory Template Methods Prototypes

References Design Patterns, Elements of Reusable Object-Oriented Software, Erich Gamma, et. al., Addison-Wesley, 1994, ISBN 0-201-63361-2 http://www.ondotnet.com/pub/a/dotnet/2003 /08/11/factorypattern.html http://en.wikibooks.org/wiki/More_C%2B%2 B_Idioms/Virtual_Constructor