Factory Method - PowerPoint PPT Presentation

About This Presentation
Title:

Factory Method

Description:

Prototypes don't subclass the Creator, but often need to initialize the Product class. Creator would use Initialize on the object, but Factory Method doesn't need this ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 15
Provided by: chriscol
Learn more at: http://www.cs.rpi.edu
Category:

less

Transcript and Presenter's Notes

Title: Factory Method


1
Factory Method
Also known as Virtual Constructor
  • Chris Colasuonno

2
Factory Method Defined
  • Define an interface for creating an object, but
    let the subclasses decide which class to
    instantiate.
  • (Factory Method lets a class defer instantiation
    to subclasses)
  • (Gamma et al. 107)

3
Factory Method Defined
  • Creational

4
Factory Method Motivated
Application
Document
CreateDocument() NewDocument() Open Document()
Document docCreateDocument() docs.Add(doc) doc
-gtOpen()
Open() Close() Save()
MyApplication
MyDocument
CreateDocument()
Return new MyDocument
5
Factory Method Motivated
6
Factory Method Applied
  • When should we use Factory Method?
  • When a class
  • Cant predict the class of the objects it needs
    to create
  • Wants its subclasses to specify the objects that
    it creates
  • Delegates responsibility to one of multiple
    helper subclasses, and you need to localize the
    knowledge of which helper is the delagte

7
Factory Method The Consequences
  • Advantage
  • Elminates the need to bind application-specific
    classes to your code.
  • Code deals only with the Product interface
    (Document), so it can work with any user-defined
    ConcreteProduct (MyDocument)

8
Factory Method The Consequences
  • Potential Disadvantages
  • Clients may have to make a subclass of the
    Creator, just so they can create a certain
    ConcreteProduct.
  • This would be acceptable if the client has to
    subclass the Creator anyway, but if not then the
    client has to deal with another point of
    evolution.

9
Factory Method The Consequences
  • In addition
  • Provides hooks for subclasses
  • Connects parallel class hierarchies
  • P.109-110

10
Factory Method Implemented
  • Two major varieties
  • Parameterized factory methods
  • Language-specific issues
  • Using templates to avoid subclassing
  • Naming conventions

11
Factory Method Known Uses
  • Frameworks
  • Toolkits

12
Factory Method Related Patterns
  • Abstract Factory is often implemented with
    Factory Method.
  • Usually called in Template Methods
  • Prototypes dont subclass the Creator, but often
    need to initialize the Product class. Creator
    would use Initialize on the object, but Factory
    Method doesnt need this operation.

13
Sources
  • Gammal et al. Design Patterns Elements of
    Reusable Object-Oriented Software.
    Addison-Wesley. 1995.
  • Data and Object Factory. Image Factory.gif
    Internet. Available by HTTP http//www.dofactory
    .com/patterns/PatternFactory.aspx

14
Factory Method Notes
  • Text Books pages 107-116
  • Slides available at www.rpi.edu/colasc
Write a Comment
User Comments (0)
About PowerShow.com