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.

A small analogy to understand Factory method better. Your code is the pasta maker,different disks create different pasta shapes:these are the factories. All disks have certain properties in common,so that they will work with the pasta maker.All pastas have certain characteristics in common that are inherited from the generic “Pasta” object.

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 Product class and can work with any ConcreteProduct 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

External Links and References