The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.

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

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.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Plab – Tirgul 12 Design Patterns
ITEC200 – Week03 Inheritance and Class Hierarchies.
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
Feb Ron McFadyen1 Factory Method Iterator Example : Java collection classes represent an example of the Factory Method design pattern. The.
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
Nov, 1, Design Patterns PROBLEM CONTEXT SOLUTION A design pattern documents a proven solution to a recurring problem in a specific context and its.
Factory Method By Judith Mziray And Jerry Cipolla.
Design Patterns I 1. Creational Pattern Singleton: intent and structure Ensure a class has one instance, and provide a global point of access to it 2.
Template Method By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
DESIGN PATTERNS Redesigning Applications And
1 Creational Patterns CS : Software Design Winter /T8.
Design Patterns Examples in C++ Moshe Fresko Bar-Ilan University Object Oriented Programming
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.
7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.
Pattern Abstract Factory
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Design Patterns.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
02 - Creational Design Patterns Moshe Fresko Bar-Ilan University תשס"ח 2008.
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.
Creational Patterns CSE Creational Patterns Class creational pattern ◦ uses inheritance to vary the class that is instantiated Object creational.
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.
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.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns I.
The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
The Factory Pattern Sanjay Yadav (ISE ).
Advanced Object-oriented Design Patterns Creational Design Patterns.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
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.
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.
Design Patterns: MORE Examples
Abstract Factory Pattern
Factory Method Pattern
Design Pattern Catalogues
Low Budget Productions, LLC
Factory Patterns 1.
Software Design and Architecture
Software Design and Architecture
Factory Method Pattern
Software Engineering Lecture 7 - Design Patterns
CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”
UNIT-III Creational Patterns UNIT-III.
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Lesson 5: More on Creational Patterns
Creational Patterns.
Design by Abstraction (Continuation) CS 3331 Spring 2005
Presentation transcript:

The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation – In a language which distinguishes between class and type, there must be a mechanism for creation of an object which would reveal its type, but not its class. – Unfortunately, in Java, the best known language in which such a separation exists, there is no such mechanism. Abstract Methods deal exactly with this problem. The Abstract Factory Generalizes. Pattern Intent: Define an interface for creating an object, but let subclasses decide which class to instantiate. Lets a class defer instantiation to subclasses

Factory Method Motivation

Factory Method pattern Motivation Document* doc = CreateDocument(); docs.Add(doc); doc->Open(); Application CreateDocument() NewDocument() OpenDocument() Document Open() Save() Close() Revert() docs MyDocument MyApplication CreateDocument() creates return new MyDocument

Factory Method Structure Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Factory Method Consequences

Factory Method pattern Motivation (cont.) Application class is responsible for creation (and management) of Documents Problem: – Application class knows: WHEN a new document should be created – Application class doesn’t know: WHAT KIND of document to create Solution: – Application subclasses redefine abstract CreateDocument() method to return an appropriate Document subclass instance

Connecting parallel hierarchies LineFigure CreateManipulator() TextFigure CreateManipulator() LineManipulator DownClick() Drag() TextManipulator DownClick() Drag() Manipulator DownClick() Drag() Client Figure CreateManipulator() creates Localizes knowledge of which classes belongs together

Factory Method pattern Structure Product ConcreteProduct Creator FactoryMethod() AnOperation() ConcreteCreator FactoryMethod() creates... product= FactoryMethod()... return new ConcreteProduct

Factory Method pattern Applicability Use the Factory Method pattern when – a class can’t anticipate the class of objects it must create – a class wants its subclasses to specify the object it creates – classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate

Factory Method pattern Participants Product (Document) – defines the interface of objects the factory method creates ConcreteProduct (MyDocument) – implements the Product interface Creator (Application) – declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object. – may call the factory method to create a Product object ConcreteCreator (MyApplication) – overrides the factory method to return an instance of a ConcreteProduct

Factory Method - Consequences Advantage – eliminates the need to bind application specific classes into your code; your code deals with Product interface implemented by ConcreteProduct subclasses Potential disadvantage – clients might have to subclass the Creator class just to create a particular (I.e., 1) ConcreteProduct object Provides hooks for subclasses – Factory Method gives subclasses a hook for providing an extended version of an object Connects parallel class hierarchies – see next slide for example

Connecting parallel hierarchies LineFigure CreateManipulator() TextFigure CreateManipulator() LineManipulator DownClick() Drag() TextManipulator DownClick() Drag() Manipulator DownClick() Drag() Client Figure CreateManipulator() creates Localizes knowledge of which classes belongs together

Factory Method - Implementation Two major varieties – Creator declares ABSTRACT factory method, ConcreteCreator implements it – Creator defines a default implementation for factory method Parameterized factory methods – lets the factory method to create multiple kinds of objects – factory methods takes a parameter: a kind of object to create – all products have to share a Product interface

Parameterized factory methods class Creator { public: virtual Product* Create(ProductId); }; Product* Creator::Create(ProductId id) { if (id == MINE) return new MyProduct; if (id == YOURS) return new YourProduct; return 0; } Product* MyCreator::Create(ProductId id) { if (id == THEIRS) return new TheirProduct; return Creator::Create(id); }

Language-specific variants Smalltalk Class Creator … factoryMethod ^self productClass new … Class ConcreteCreator … productClass ^ConcreteProduct

Language-specific variants C++ “Lazy evaluation” class Creator { public: Product* GetProduct(); protected: virtual Product* CreateProduct(); private: Product* _product; }; Product* Creator::GetProduct() { if (_product == 0) _product = CreateProduct(); return _product; }

Using templates to avoid subclassing class Creator { public: virtual Product* CreateProduct() = 0; }; template class StandartCreator : public Creator { public: virtual Product* CreateProduct(); }; template Product* StandartCreatot ::CreateProduct(){ return new TheProduct; }