Docsity

Factory Method Design Pattern: Implementation and Applications, Lecture notes of Design Patterns

Partial preview of the text

Download Factory Method Design Pattern: Implementation and Applications and more Lecture notes Design Patterns in PDF only on Docsity!

FACTORY METHOD

Intent

 “Define an interface for creating an object, but let subclasses decide which class to instantiate” It lets a class defer instantiation to subclasses at run time. It refers to the newly created object through a common interface.

Motivating Examples

 Hotel: One good example for the Factory Method is the Hotel. When Check in, the person gets the key from the front desk where he can be looked up as the ‘room’ factory. Now if he wants to make a phone call to some outside office, he will contact the front desk to make the connection to that call which means the front desk is acting as an interface.

Motivating Examples -

Cont.

 Frameworks: It is used in frameworks where library code needs to create objects of types which may be sub classed by applications using the framework since the library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library in the framework.

Forces

 It is required to have a set of reusable classes which are flexible enough to be extended. The client does not know the type of object that need to be created in advance and still want to perform operations on them.

Applicability

 Factory Method is needed when: A class can’t anticipate the class of objects it must create. A class wants its subclasses to specify the objects it creates. Localization of knowledge of helper sub classes which the classes want to delegate.

Participants

Product(IHttpHandler) Defines the interface of objects the factory method creates. ConcreteProduct(ASP.SamplePage_aspx) Implements the Product Interface Creator(IHttpHandlerFactory) Declares the factory method and may provide a default implementation for it. It returns an object of type Product. ConcreteCreator(PageHandlerFactory) Overrides the factory method to return an instance of ConcreteProduct.

Collaborators

The Creator relies on the subclass’s factory method and return instance of appropriate ConcreteProduct object. The Creator executes some sequence of operations on the object.

Consequences – Cont.

Implementation

 Two major varieties Abstract Creator class with no default implementation Concrete Creator with default implementation. Solutions: Parameterized Methods Templates

Templatized Factory

Methods

class Creator { public: Creator() { // You cannot call the factory method here (why?) // Use lazy initialization instead } virtual Product* CreateProduct() = 0; }; template class StandardCreator: public Creator { public: virtual Product* CreateProduct() { return new T; } } // In the Client StandardCreator myCreator;

Known Uses

It is a very pervasive pattern. It is used in several places in the Java API. For example, URLConnection has a method getContent that returns the content as an appropriate object (html, gif etc.) FCL (.Net Framework Class Library) is no exception to the use of Factory Method Pattern. For example, it is used in Systems.Collections.IEnumerable, System.Net.WebRequest, System.Security.Cryptography

Basic Example

References

 Design Patterns, Elements of Reusable Object-Oriented Software, Erich Gamma, et. al., Addison-Wesley, 1994, ISBN 0-201-63361- http://www.ondotnet.com/pub/a/do tnet/2003/08/11/factorypattern.h tml http://en.wikibooks.org/wiki/Mor e_C%2B%2B_Idioms/Virtual_Constru ctor