Content deleted Content added
No edit summary Tags: Reverted possible vandalism references removed Disambiguation links added |
Undid revision 1161688732 by 173.61.86.226 (talk) Tags: Undo Mobile edit Mobile web edit Advanced mobile edit |
||
Line 1:
{{Short description|Object-oriented software design pattern}}
In [[class-based programming]], the '''factory method pattern''' is a [[creational pattern]] that uses factory methods to deal with the problem of [[object creation|creating objects]] without having to specify the exact [[class (computer programming)|class]] of the object that will be created. This is done by creating objects by calling a factory method—either specified in an [[Interface (object-oriented programming)|interface]] and implemented by child classes, or implemented in a base class and optionally [[method overriding|overridden]] by derived classes—rather than by calling a [[Constructor (object-oriented programming)|constructor]].
==Overview==
The Factory Method
<ref name="GoF">{{cite book|author1=Erich Gamma |author2= Richard Helm |author3= Ralph Johnson|author4= John Vlissides|title=Design Patterns: Elements of Reusable Object-Oriented Software|year=1994|publisher=Addison Wesley|isbn=0-201-63361-2|pages=121ff}}</ref>
design pattern is one of the twenty-three well-known ''[[Design Patterns|design patterns]]'' that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.
The Factory Method design pattern solves problems like:
<ref>{{cite web|title=The Factory Method design pattern - Problem, Solution, and Applicability|url=http://w3sdesign.com/?gr=c03&ugr=proble|website=w3sDesign.com|accessdate=2017-08-17}}</ref>
* How can an object be created so that subclasses can redefine which class to instantiate?
* How can a class defer instantiation to subclasses?
The Factory Method design pattern describes how to solve such problems:
* Define a separate operation (''factory method'') for creating an object.
* Create an object by calling a ''factory method''.
This enables writing of subclasses to change the way an object is created (to redefine which class to instantiate).<br>
See also the UML class diagram below.
==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 (software)|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 [[concern (computer science)|concerns]]. The factory method design pattern handles these problems by defining a separate [[method (computer science)|method]] for creating the objects, which [[subclass (computer science)|subclasses]] can then override to specify the [[Subtyping|derived type]] of product that will be created.
The factory method pattern relies on inheritance, as object creation is delegated to subclasses that implement the factory method to create objects.<ref>{{cite book
| last1 = Freeman
| first1 = Eric
| last2 = Freeman
| first2 = Elisabeth
| last3 = Kathy
| first3 = Sierra
| last4 = Bert
| first4 = Bates
| editor-last1 = Hendrickson
| editor-first1 = Mike
| editor-last2 = Loukides
| editor-first2 = Mike
| year = 2004
| title = Head First Design Patterns
| volume
| page = 162
| publisher = O'REILLY
| format
| isbn = 978-0-596-00712-6
| access-date = 2012-09-12
| url = http://shop.oreilly.com/product/9780596007126.do
}}</ref>
As shown in the C# example below, the factory method pattern can also rely on an Interface - in this case IPerson - to be implemented.
==
===
[[File:w3sDesign Factory Method Design Pattern UML.jpg|frame|none|A sample UML class diagram for the Factory Method design pattern.
<ref>{{cite web|title=The Factory Method design pattern - Structure and Collaboration|url=http://w3sdesign.com/?gr=c03&ugr=struct|website=w3sDesign.com|access-date=2017-08-12}}</ref>]]
In the above [[Unified Modeling Language|UML]] [[class diagram]],
the <code>Creator</code> class that requires a <code>Product</code> object does not instantiate the <code>Product1</code> class directly.
Instead, the <code>Creator</code> refers to a separate <code>factoryMethod()</code> to create a product object,
which makes the <code>Creator</code> independent of which concrete class is instantiated.
Subclasses of <code>Creator</code> can redefine which class to instantiate. In this example, the <code>Creator1</code> subclass implements the abstract <code>factoryMethod()</code> by instantiating the <code>Product1</code> class.
== Examples ==
This C++14 implementation is based on the pre C++98 implementation in the book.<ref>{{cite book |author=Erich Gamma |title=Design Patterns: Elements of Reusable Object-Oriented Software |publisher=Addison Wesley |year=1994 |isbn=0-201-63361-2 |pages=123 ff.}}</ref>
<syntaxhighlight lang="c++">
#include <iostream>
#include <memory>
enum ProductId {MINE, YOURS};
// defines the interface of objects the factory method creates.
class Product {
public:
virtual void print() = 0;
virtual ~Product() = default;
};
// implements the Product interface.
class ConcreteProductMINE: public Product {
public:
void print() {
std::cout << "this=" << this << " print MINE\n";
}
};
// implements the Product interface.
class ConcreteProductYOURS: public Product {
public:
void print() {
std::cout << "this=" << this << " print YOURS\n";
}
};
// declares the factory method, which returns an object of type Product.
class Creator {
public:
virtual std::unique_ptr<Product> create(ProductId id) {
if (ProductId::MINE == id) return std::make_unique<ConcreteProductMINE>();
if (ProductId::YOURS == id) return std::make_unique<ConcreteProductYOURS>();
// repeat for remaining products...
return nullptr;
}
virtual ~Creator() = default;
};
int main() {
// The unique_ptr prevent memory leaks.
std::unique_ptr<Creator> creator = std::make_unique<Creator>();
std::unique_ptr<Product> product = creator->create(ProductId::MINE);
product->print();
product = creator->create(ProductId::YOURS);
product->print();
}
</syntaxhighlight>
The program output is like
<syntaxhighlight lang="c++">
this=0x6e5e90 print MINE
this=0x6e62c0 print YOURS
</syntaxhighlight>
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.
=== Structure ===
[[File:New WikiFactoryMethod.png|734x734px|center]]
<code>Room</code> is the base class for a final product (<code>MagicRoom</code> or <code>OrdinaryRoom</code>). <code>MazeGame</code> declares the abstract factory method to produce such a base product. <code>MagicRoom</code> and <code>OrdinaryRoom</code> are subclasses of the base product implementing the final product. <code>MagicMazeGame</code> and <code>OrdinaryMazeGame</code> are subclasses of <code>MazeGame</code> implementing the factory method producing the final products. Thus factory methods decouple callers (<code>MazeGame</code>) from the implementation of the concrete classes. This makes the "new" Operator redundant, allows adherence to the [[Open/closed principle]] and makes the final product more flexible in the event of change.
=== Example implementations ===
====[[C Sharp (programming language)|C#]]====
<syntaxhighlight lang="csharp">
// Empty vocabulary of actual object
public interface IPerson
{
}
public class
{
public string GetName()
{
}
}
public class
{
public string GetName()
{
}
}
public
{
Rural,
Urban
}
/// <summary>
/// Implementation of Factory - Used to create objects.
/// </summary>
public class PersonFactory
{
public IPerson GetPerson(PersonType type)
{
{
return new Villager();
case PersonType.Urban:
return new CityPerson();
default:
throw new NotSupportedException();
}
}
}
</syntaxhighlight>
In the above code you can see the creation of one interface called <code>IPerson</code> and two implementations called <code>Villager</code> and <code>CityPerson</code>. Based on the type passed into the <code>PersonFactory</code> object, we are returning the original concrete object as the interface <code>IPerson</code>.
A factory method is just an addition to <code>PersonFactory</code> 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.
<syntaxhighlight lang="csharp">
public interface IProduct
{
string GetName();
bool SetPrice(double price);
}
public class
{
public string GetName()
{
}
public
{
return true;
}
}
/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
protected abstract IProduct MakeProduct();
public IProduct GetObject() // Implementation of Factory Method.
{
return
}
}
public class
{
protected override IProduct MakeProduct()
{
IProduct product = new Phone();
// Do something with the object after you get the object.
product.SetPrice(20.30);
}
}
</syntaxhighlight>
You can see we have used <code>MakeProduct</code> in concreteFactory. As a result, you can easily call <code>MakeProduct()</code> from it to get the <code>IProduct</code>. 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.
====[[Java (programming language)|Java]]====
This [[Java (programming language)|Java]] example is similar to one in the book ''[[Design Patterns]].''
[[File:Maze game UML.svg]]
The MazeGame uses Rooms but it puts the responsibility of creating Rooms to its subclasses which create the concrete classes. The regular game mode could use this template method:
<syntaxhighlight lang="java">
public abstract class Room {
abstract void connect(Room room);
}
public class
public
}
public class
public
}
public abstract class MazeGame {
private final List<Room> rooms = new ArrayList<>();
public MazeGame() {
Room room1 = makeRoom();
Room room2 = makeRoom();
room1.connect(room2);
rooms.add(room1);
rooms.add(room2);
}
abstract protected Room makeRoom();
}
</syntaxhighlight>
In the above snippet, the <code>MazeGame</code> constructor is a [[Template method pattern|template method]] that makes some common logic. It refers to the <code>makeRoom</code> 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 <code>makeRoom</code> method:
<syntaxhighlight lang="java">
public class MagicMazeGame extends MazeGame {
@Override
protected MagicRoom makeRoom() {
return new
}
}
public class OrdinaryMazeGame extends MazeGame {
protected OrdinaryRoom makeRoom() {
return new OrdinaryRoom();
}
}
MazeGame ordinaryGame = new OrdinaryMazeGame();
MazeGame magicGame = new MagicMazeGame();
</syntaxhighlight>
===
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 with the Java example above).
<syntaxhighlight lang="php">
/* Factory and car interfaces */
interface CarFactory
{
public function makeCar(): Car;
}
interface Car
{
public function getType(): string;
}
/* Concrete implementations of the factory and car */
class SedanFactory implements CarFactory
{
public function makeCar(): Car
{
return
}
}
class Sedan implements Car
{
public function getType(): string
{
}
}
/* Client */
$factory = new SedanFactory();
$car = $factory->makeCar();
print $car->getType();
</syntaxhighlight>
==== [[Python (programming language)|Python]] ====
Same as Java example.
<syntaxhighlight lang="python3">
from abc import ABC, abstractmethod
class MazeGame(ABC):
def __init__(self) -> None:
self.rooms = []
self._prepare_rooms()
def _prepare_rooms(self) -> None:
room1 = self.make_room()
room2 = self.make_room()
room1.connect(room2)
self.rooms.append(room1)
self.rooms.append(room2)
def play(self) -> None:
print(f"Playing using {self.rooms[0]}")
@abstractmethod
def make_room(self):
raise NotImplementedError("You should implement this!")
class MagicMazeGame(MazeGame):
def make_room(self) -> "MagicRoom":
return MagicRoom()
class OrdinaryMazeGame(MazeGame):
def make_room(self) -> "OrdinaryRoom":
return OrdinaryRoom()
class Room(ABC):
def __init__(self) -> None:
self.connected_rooms = []
def connect(self, room: "Room") -> None:
self.connected_rooms.append(room)
class MagicRoom(Room):
def __str__(self) -> str:
return "Magic room"
class OrdinaryRoom(Room):
def __str__(self) -> str:
return "Ordinary room"
ordinaryGame = OrdinaryMazeGame()
ordinaryGame.play()
magicGame = MagicMazeGame()
magicGame.play()
</syntaxhighlight>
==Uses==
* In [[ADO.NET]], [
* In [[Qt (toolkit)|Qt]], [http://
* In [[Java (programming language)|Java]], several factories are used in the [http://
* In the [[HTML5]] [[Document Object Model|DOM]] [[application programming interface|API]], the Document interface contains a createElement factory method for creating specific elements of the HTMLElement interface.
==See also==
* ''[[Design Patterns]]'', the highly
* [[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==
{{Reflist}}
*{{cite book
| author1=Martin Fowler | author2-link=Kent Beck | author2=Kent Beck | author3-link=John Brant (author) | author3=John Brant | author4-link=William Opdyke | author4=William Opdyke | author5-link=Don Roberts (author) | author5=Don Roberts
| title = Refactoring: Improving the Design of Existing Code
| publisher = Addison-Wesley
|
| isbn = 0-201-48567-2
| author1-link=Martin Fowler (software engineer) }}
*{{cite book |author1=Gamma, Erich |author2-link=Richard Helm |author2=Helm, Richard |author3=Johnson, Ralph |author4=Vlissides, John |
title=Design Patterns: Elements of Reusable Object-Oriented Software |
publisher=Addison-Wesley |
year=1994 |
isbn=0-201-63361-2
|author1-link=Erich Gamma |title-link=Design Patterns }}
*{{cite book |author1=Cox, Brad J. |
title=Object-oriented programming: an evolutionary approach |
publisher=Addison-Wesley |
year=1986 |
isbn=978-0-201-10393-9
|
title-link=Object-oriented programming }}
*{{cite journal |
last=Cohen |
first=Tal |author2=Gil, Joseph |
title=Better Construction with Factories |
journal=Journal of Object Technology |
volume=6 |
issue=6 |
pages=103 |
year=2007 |
publisher=[[Bertrand Meyer]] |
url=http://tal.forum2.org/static/cv/Factories.pdf |
doi=10.5381/jot.2007.6.6.a3 |
doi-access=free }}
==External links==
{{Wikibooks
| 1 = Computer Science Design Patterns
| 2 = Factory method examples
}}
* [http://
* [https://web.archive.org/web/20080503082912/http://www.lepus.org.uk/ref/companion/FactoryMethod.xml Factory method in UML and in LePUS3] (a Design Description Language)
* [http://drdobbs.com/java/208403883 Consider static factory methods] by Joshua Bloch
{{Design Patterns Patterns}}
{{DEFAULTSORT:Factory Method Pattern}}
<!--Categories-->
[[Category:Software design patterns]]
[[Category:Articles with example Java code]]
[[Category:
[[Category:
[[Category:Articles with example PHP code]]
|