CSC/ECE 517 Fall 2009/wiki3 16 AD

From Expertiza_Wiki
Jump to navigation Jump to search

Definition

Factory Method is a creational pattern. This pattern helps to model an interface for creating an object which at creation time can let its subclasses decide which class to instantiate.It is called as Factory Pattern as it is responsible for "Manufacturing" an Object. It helps instantiate the appropriate Subclass by creating the right Object from a group of related classes. The Factory Pattern encourages loose coupling by eliminating the need to bind application-specific classes into the code.It Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Overview

This topic deals with the Factory method design pattern.It explains Factory design pattern and compares with other creational pattern to highlight the uniqueness and distinguishing features of the pattern.It also gives examples of where factory design pattern is used and scenarios wherein its more suitable than other creational patterns.


In Depth Analysis

Factory Patterns Insight

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.It means that Factory is written without knowing what actual sub class will be instantiated. The subclass which is instantiated is determined solely by which subclass is instantiated and used by the application.

Factory method framework can be shown in the figure below.In the below figure the createDocument() method is a factory method.

Benefits Code is made more flexible and reusable by the elimination of instantiation of application-specific classes,Code deals only with the interface of the Factory class and can work with any sub class that supports this interface

Applicants of Factory Pattern

Examples of Factory Pattern

Comparison with other Creational Patterns

Comparison with Builder Pattern

In Factory pattern, the factory is in charge of creating various subtypes of an object depending on the needs.

Where as in Builder pattern, the composition of the objects might differ within the same subclass.

An example of a Factory method CarCreate might return a Audi or a Toyota where as in the same scenario if we are using a Builder pattern, it allows us to create objects with finer granularity like specifying different engine specifications (a 4 cylinder engine or a 6 cylinder engine)for the objects. As seen from the above example the Factory pattern is a simpler version of the Builder Pattern.

UML diagram of Builder Pattern

Source: [1]

Comparison with Prototype Pattern

The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it is dealing with as long as the concrete prototype extends or implements the prototype interface/class. The concrete prototype object is responsible for cloning itself and returning the cloned object. The pattern enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at runtime as long as they conform to the abstract prototype.

Comparison with Abstract Factory Pattern

Abstract pattern is one level of abstraction higher than the factory pattern. A Factory pattern is one that returns an instance of one of several possible classes depending on the data provided to it. Usually all of the classes it returns have a common parent class and common methods, but each of them performs a task differently and is optimized for different kinds of data.

There are several similar variations on the factory pattern:

 1. The base class is abstract and the pattern must return a complete working class. 
 2. The base class contains default methods and is only sub-classed for cases where the default methods are insufficient.
 3. Parameters are passed to the factory telling it which of several class types to return. In this case the classes may share
    the same method names but may do something quite different.

External Links and References