Factory Method Also known as Virtual Constructor Chris

  • Slides: 14
Download presentation
Factory Method Also known as “Virtual Constructor” Chris Colasuonno

Factory Method Also known as “Virtual Constructor” Chris Colasuonno

Factory Method: Defined o o o Define an interface for creating an object, but

Factory Method: Defined o o o 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)

Factory Method: Defined o Creational

Factory Method: Defined o Creational

Factory Method: Motivated Document Open() Close() Save() Application Create. Document() New. Document() Open Document()

Factory Method: Motivated Document Open() Close() Save() Application Create. Document() New. Document() Open Document() Document* doc=Create. Document(); docs. Add(doc); doc->Open(); My. Application My. Document Create. Document() Return new My. Document

Factory Method: Motivated

Factory Method: Motivated

Factory Method: Applied o o When should we use Factory Method? When a class:

Factory Method: Applied o o When should we use Factory Method? When a class: n n n Can’t 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

Factory Method: The Consequences o Advantage: n n Elminates the need to bind applicationspecific

Factory Method: The Consequences o Advantage: n n Elminates the need to bind applicationspecific classes to your code. Code deals only with the Product interface (Document), so it can work with any user-defined Concrete. Product (My. Document)

Factory Method: The Consequences o Potential Disadvantages n n Clients may have to make

Factory Method: The Consequences o Potential Disadvantages n n Clients may have to make a subclass of the Creator, just so they can create a certain Concrete. Product. 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.

Factory Method: The Consequences o In addition: n n n “Provides hooks for subclasses”

Factory Method: The Consequences o In addition: n n n “Provides hooks for subclasses” “Connects parallel class hierarchies” P. 109 -110

Factory Method: Implemented o o o Two major varieties Parameterized factory methods Language-specific issues

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

Factory Method: Known Uses o o Frameworks Toolkits

Factory Method: Known Uses o o Frameworks Toolkits

Factory Method: Related Patterns o o o Abstract Factory is often implemented with Factory

Factory Method: Related Patterns o o o Abstract Factory is often implemented with Factory Method. Usually called in Template Methods 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 operation.

Sources o o Gammal et al. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley.

Sources o o 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/Pattern. Factory. aspx

Factory Method: Notes o o Text Books pages 107 -116 Slides available at www.

Factory Method: Notes o o Text Books pages 107 -116 Slides available at www. rpi. edu/~colasc