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.

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

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

Comparison with Abstract Factory Pattern

External Links and References