Factory method pattern
In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor.
Definition
"Define an interface for creating an object, but let subclasses decide which class to instantiate. The Factory method lets a class defer instantiation it uses to subclasses." (Gang Of Four)
Creating 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. The factory method design pattern handles these problems by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created.
The factory method pattern may rely on inheritance, as object creation is delegated to subclasses that implement the factory method to create objects.[1]
Example implementations
Java
A maze game may be played in two modes, one with regular rooms that are only connected with adjacent rooms, and one with magic rooms that allow players to be transported at random (this Java example is similar to one in the book Design Patterns). The regular game mode could use this template method:
public abstract class MazeGame {
public MazeGame() {
Room room1 = makeRoom();
Room room2 = makeRoom();
room1.connect(room2);
}
abstract protected Room makeRoom();
}
In the above snippet, the MazeGame
constructor is a template method that makes some common logic. It refers to the makeRoom
factory method that encapsulates the creation of rooms such that other rooms can be used in a subclass. To implement the other game mode that has magic rooms, it suffices to override the makeRoom
method:
public class MagicMazeGame extends MazeGame {
@Override
protected Room makeRoom() {
return new MagicRoom();
}
}
public class OrdinaryMazeGame extends MazeGame {
@Override
protected Room makeRoom() {
return new OrdinaryRoom();
}
}
MazeGame ordinaryGame = new OrdinaryMazeGame();
MazeGame magicGame = new MagicMazeGame();
Delphi/Object Pascal
A board game has two modes, Chess and Checkers, selectable by the user before the game starts.
The generic board class, to be used as template for each board type:
TBoardGame = Class
public
procedure makeBoard; Virtual; Abstract;
End;
The specific board classes:
TChessGame = Class (TBoardGame)
public
procedure makeBoard; Override;
End;
TCheckersGame = Class (TBoardGame)
public
procedure makeBoard; Override;
End;
And the factory class:
// Interface
TBoardGamesFactory = Class
public
class function CreateBoardGame(GameType: TBoardGameType): TBoardGame;
End;
// Implementation
class function TBoardGamesFactory.CreateBoardGame(GameType: TBoardGameType): TBoardGame;
begin
case GameType of
bgChess : Result := TChessGame.Create;
bgCheckers: Result := TCheckersGame.Create;
end;
end;
Would be instantiated like this:
var
Game: TBoardGame;
begin
Game := TBoardGamesFactory.CreateBoardGame(bgCheckers); // This TBoardGame variable will in fact be a TCheckersGame, created by the factory
Game.makeBoard;
PHP
Another example in PHP follows, this time using interface implementations as opposed to subclassing (however the same can be achieved through subclassing). It is important to note that the factory method can also be defined as public and called directly by the client code (in contrast the Java example above).
/* Factory and car interfaces */
interface CarFactory
{
public function makeCar();
}
interface Car
{
public function getType();
}
/* Concrete implementations of the factory and car */
class SedanFactory implements CarFactory
{
public function makeCar()
{
return new Sedan();
}
}
class Sedan implements Car
{
public function getType()
{
return 'Sedan';
}
}
/* Client */
$factory = new SedanFactory();
$car = $factory->makeCar();
print $car->getType();
C#
Factory pattern deals with the instantiation of object without exposing the instantiation logic. In other words, a Factory is actually a creator of objects which have a common interface.
//Empty vocabulary of Actual object
public interface IPeople
{
string GetName();
}
public class Villagers : IPeople
{
public string GetName()
{
return "Village Person";
}
}
public class CityPeople : IPeople
{
public string GetName()
{
return "City Person";
}
}
public enum PeopleType
{
Rural,
Urban
}
/// <summary>
/// Implementation of Factory - Used to create objects
/// </summary>
public class Factory
{
public IPeople GetPeople(PeopleType type)
{
switch (type)
{
case PeopleType.Rural:
return new Villagers();
case PeopleType.Urban:
return new CityPeople();
default:
throw new NotSupportedException();
}
}
}
In the above code you can see the creation of one interface called IPeople and implemented two classes from it as Villagers and CityPeople. Based on the type passed into the factory object, we are sending back the original concrete object as the Interface IPeople.
A Factory method is just an addition to Factory class. It creates the object of the class through interfaces but on the other hand, it also lets the subclass decide which class is instantiated.
public interface IProduct
{
string GetName();
string SetPrice(double price);
}
public class Phone : IProduct
{
private double _price;
#region Product Members
public string GetName()
{
return "Apple TouchPad";
}
public string SetPrice(double price)
{
this._price = price;
return "success";
}
#endregion
}
/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
protected abstract IProduct DoSomething();
public IProduct GetObject() // Implementation of Factory Method.
{
return this.DoSomething();
}
}
public class PhoneConcreteFactory : ProductAbstractFactory
{
protected override IProduct DoSomething()
{
IProduct product = new Phone();
//Do something with the object after you get the object.
product.SetPrice(20.30);
return product;
}
}
You can see we have used GetObject in concreteFactory. As a result, you can easily call DoSomething() from it to get the IProduct. You might also write your custom logic after getting the object in the concrete Factory Method. The GetObject is made abstract in the Factory interface.
Limitations
There are three limitations associated with the use of the factory method. The first relates to refactoring existing code; the other two relate to extending a class.
- The first limitation is that refactoring an existing class to use factories breaks existing clients. For example, if class Complex were a standard class, it might have numerous clients with code like:
Complex c = new Complex(-1, 0);
- Once we realize that two different factories are needed, we change the class (to the code shown earlier). But since the constructor is now private, the existing client code no longer compiles.
- The second limitation is that, since the pattern relies on using a private constructor, the class cannot be extended. Any subclass must invoke the inherited constructor, but this cannot be done if that constructor is private.
- The third limitation is that, if the class were to be extended (e.g., by making the constructor protected—this is risky but feasible), the subclass must provide its own re-implementation of all factory methods with exactly the same signatures. For example, if class
StrangeComplex
extendsComplex
, then unlessStrangeComplex
provides its own version of all factory methods, the callStrangeComplex.fromPolar(1, pi);
Complex
(the superclass) rather than the expected instance of the subclass. The reflection features of some languages can avoid this issue.
All three problems could be alleviated by altering the underlying programming language to make factories first-class class members (see also Virtual class).[2]
Uses
- In ADO.NET, IDbCommand.CreateParameter is an example of the use of factory method to connect parallel class hierarchies.
- In Qt, QMainWindow::createPopupMenu is a factory method declared in a framework that can be overridden in application code.
- In Java, several factories are used in the javax.xml.parsers package. e.g. javax.xml.parsers.DocumentBuilderFactory or javax.xml.parsers.SAXParserFactory.
See also
- Design Patterns, the highly influential book
- Design pattern, overview of design patterns in general
- Abstract factory pattern, a pattern often implemented using factory methods
- Builder pattern, another creational pattern
- Template method pattern, which may call factory methods
- Joshua Bloch's idea of a static factory method, which he says has no direct equivalent in Design Patterns.
References
- ↑ Freeman, Eric; Freeman, Elisabeth; Kathy, Sierra; Bert, Bates (2004). Hendrickson, Mike; Loukides, Mike, eds. "Head First Design Patterns" (paperback) 1. O'REILLY: 162. ISBN 978-0-596-00712-6. Retrieved 2012-09-12.
- ↑ Agerbo, Ellen; Cornils, Aino (1998). "How to preserve the benefits of design patterns". Conference on Object Oriented Programming Systems Languages and Applications (Vancouver, British Columbia, Canada: ACM): 134–143. ISBN 1-58113-005-8.
- Martin Fowler; Kent Beck; John Brant; William Opdyke; Don Roberts (June 1999). Refactoring: Improving the Design of Existing Code. Addison-Wesley. ISBN 0-201-48567-2.
- Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. ISBN 0-201-63361-2.
- Cox, Brad J.; (1986). Object-oriented programming: an evolutionary approach. Addison-Wesley. ISBN 978-0-201-10393-9.
- Cohen, Tal; Gil, Joseph (2007). "Better Construction with Factories" (PDF). Journal of Object Technology (Bertrand Meyer). Retrieved 2007-03-12.
External links
The Wikibook Computer Science Design Patterns has a page on the topic of: Factory method examples |
- Factory method in UML and in LePUS3 (a Design Description Language)
- Consider static factory methods by Joshua Bloch
- Factory Method and Magento
|