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

Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
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.
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
Creational Patterns: The Abstract Factory CSE 335 Spring 2008 E. Kraemer.
Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Design Patterns.
Creational Patterns (1) CS350, SE310, Fall, 2010.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
Software Components Creational Patterns.
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.
FacadeDesign Pattern Provide a unified interface to a set of interfaces in a subsystem. Defines a high level interface that makes the subsystem easier.
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
The Factory Pattern Sanjay Yadav (ISE ).
Advanced Object-oriented Design Patterns Creational Design Patterns.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
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.
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.
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
Strategy Design Pattern
Design Pattern Catalogues
MPCS – Advanced java Programming
Low Budget Productions, LLC
Factory Patterns 1.
Creational Pattern: Prototype
Software Design and Architecture
Flyweight Design Pattern
Factory Method 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
Object Oriented Design Patterns - Creational Patterns
CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”
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 By Anjan Kumar Bollam Professor Jim Fawcett

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 the Factory Method of initialization.

Motivating Examples Hotel: One good example for the Factory Method is the Hotel. When Check in, the person gets the key from the front desk where he can be looked up as the ‘room’ factory. Now if he wants to make a phone call to some outside office, he will contact the front desk to make the connection to that call which means the front desk is acting as an interface.

Motivating Examples - Cont. Frameworks: It 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 using the library in the framework.

Motivating Examples – Cont.

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

Applicability Factory Method is needed when: A class can’t anticipate the class of objects it must create. A class wants its subclasses to specify the objects it creates. Localization of knowledge of helper sub classes which the classes want to delegate.

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. It returns an object of type Product. ConcreteCreator(PageHandlerFactory) Overrides the factory method to return an instance of ConcreteProduct.

Collaborators The Creator relies on the subclass’s factory method and return instance of appropriate ConcreteProduct object. The Creator executes some sequence of operations on the object.

Consequences The code deals with 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 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. Solutions: 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 cannot call the 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 very 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.) FCL (.Net Framework Class Library) is no exception to the use of Factory Method Pattern. For example, it is used in Systems.Collections.IEnumerable, System.Net.WebRequest, System.Security.Cryptography

Related Patterns Abstract Factory Template Methods Prototypes

Basic Example

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/2 003/08/11/factorypattern.html http://en.wikibooks.org/wiki/More_C%2B %2B_Idioms/Virtual_Constructor