1 / 14

Factory Method

Factory Method. Also known as “Virtual Constructor”. Chris Colasuonno. 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).

rigel-head
Download Presentation

Factory Method

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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* doc=CreateDocument(); docs.Add(doc); doc->Open(); 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: • 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

  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 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.

  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

More Related