Jump to content

Factory pattern

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 139.230.245.20 (talk) at 04:54, 28 April 2008 (Structure). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
See also Factory method pattern

The Factory pattern is a creational design pattern used in software development to encapsulate the processes involved in the creation of objects.


Intent

Encapsulate the creation of an object.

Also Known As

Simple Factory pattern

Motivation

The creation of an object often requires complex processes not appropriate to include within a composing object. The object's creation may lead to a significant duplication of code, may require information not accessible to the composing object, may not provide a sufficient level of abstraction, or may otherwise not be part of the composing object's concerns.

Some of the processes required in the creation of an object include determining which object to create, managing the lifetime of the object, and managing specialized build-up and tear-down concerns of the object.


Applicability

Use the Factory pattern when

  • the creation of the object precludes reuse without significantly duplicating code.
  • the creation of the object requires the access of information or resources not appropriate to contain within the composing object.
  • the lifetime management of created objects needs to be centralized to ensure consistent behavior.






Participants

Collaborations

Consequences

Implementation

Sample Code

//Implemention of the factory pattern
protected abstract class FactoryPatternImpl{


//Provide the implementation for the methods whose behaviour wouldnt change in the subclass

public Pizza orderPizza(String argType){


Pizza pizzaInstance = createPizza(argType);

pizzaInstance.bakePizza();

pizzaInstance.doughPizza();

pizzaInstance.cutPizza();

pizzaInstance.boxPizza();

}


//Provide the implementation for the pizza methods here

public bakePizza(){

System.out.println("Bake the flour to prepare pizza");

}


public doughPizza(){

System.out.println("Dough the flour to prepare pizza");

}


public cutPizza(){

System.out.println("Cut the pizza to pack it");

}


public boxPizza(){

System.out.println("Box it");

}


//Subclass should provide the required implementation to create a pizza

protected abstract createPizza(String pizzaType);

}

Known Uses

The abstract factory pattern is similar to the factory pattern, but has the added intent of providing an interface to a factory purposed for the creation of families of related objects in order to enable variations of the factory to be used interchangeably. The abstract factory pattern is a compound pattern incorporating the use of the factory pattern and the interface pattern.

The factory method pattern is also concerned with the encapsulation of object creation details, but has the intent of providing an interface which defers the creation of objects to subclasses. The factory method pattern is also a compound pattern which incorporates the use of the Factory pattern and the interface pattern.

The builder pattern is also concerned with the encapsulation of object creation details, but has the intent of abstracting the steps involved in the construction process as opposed to encapsulating the entire creation process in one step. The factory pattern may encapsulate use of the builder pattern internally as part of the object creation process.