You are on page 1of 15

Name: Factory Method

Classification:
Scope Class
Purpose Creational
Intent
• Define an interface for creating an object,
but let subclasses decide which class to
instantiate.
• Factory Method lets a class defer
instantiation to subclasses.
Also Known As
• Virtual Constructor
Motivation
Framework – uses abstract classes, creates
objects
Ex:- A framework for applications that can
present multiple documents to the user..
What would be key abstractions in this
framework?
Application class
Document class
• Ex:-To create drawing application
– DrawingApplication
– DrawingDocument
• Responsibility of Application class?
– Managing Documents
– Create them as and when required
Motivation…..
• Problem
– A particular document subclass to instantiate is app-specific
– Application class cant predict the subclass of Document to
instantiate.
– Application class only knows when a new document should be
created, not what kind of Document to create.
• Dilemma
– The framework must instantiate classes, but it only knows about
abstract classes, which it cannot instantiate.
• Solution offered by Factory Method
– Factory Method Encapsulates the knowledge of which
Document subclass to create and moves this knowledge out of
the framework.
– Application subclasses redefine an abstract CreateDocument
operation on Application to return the appropriate Document
subclass.
Motivation….
• Once an Application subclass is
instantiated, it can then instantiate
application-specific Documents without
knowing their class
• “CreateDocument” method is called as a
factory method because it’s responsible
for “manufacturing” an object
Applicability
• Use Factory Method pattern when
– A class can’t anticipate the class of objects it
must create
– A class wants its subclasses to specify the
objects 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.
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
• ConcreteCreatory(MyApplication)
– Overrides the factory method to return an instance of a concrete
product
Collaborations
• Creator relies on its subclasses to define
the factory method so that it returns an
instance of the appropriate
ConcreteProduct
Consequences
• Eliminates the need to bind app-specific
classes in your code
• Disadvantage – clients need to subclass
for getting a particular ConcreteProduct
• Provides hooks for subclasses – extended
version of objects i.e you can always
override the factory method to determine
what kind of object you need to create.
• Connects parallel class hierarchies
Implementation
• 2 major varieties
– Fully abstract class (both Creator & Product)
– Concrete classes (both Creator & Product)
– Less known variant
• Abstract class with concrete implementation of
factory method which created a default product.
• Parameterized factory methods
– Pass the type of product to be created as a
parameter to the factory method
Known uses
• Toolkits & Frameworks
• Document example – used in MacApp
Related Patterns
• Abstract Factory – often implemented with
factory methods
• Prototype – doesn't require subleasing
Creator
• Factory methods are usually called within
Template methods

You might also like