Content deleted Content added
Squeakachu (talk | contribs) m Rollback edit(s) by 2806:261:40B:1E77:D96E:9F27:B4D4:4C1F (talk): non-constructive (RW 16.1) |
→Examples: fixed weasel word |
||
(21 intermediate revisions by 13 users not shown) | |||
Line 1:
{{Short description|Object-oriented software design pattern}}
In [[object
==Overview==
* How can an object's [[Subclass (computer science)|subclasses]] redefine its subsequent and distinct implementation? The pattern involves creation of a factory method within the [[Superclass (computer science)|superclass]] that defers the object's creation to a subclass's factory method.
* How can an object's instantiation be deferred to a subclass? Create an object by calling a factory method instead of directly calling a constructor.
This enables
▲The Factory Method design pattern solves problems like:
▲This enables writing of subclasses to change the way an object is created (to redefine which class to instantiate).<br>
==Definition==
According to ''[[Design Patterns|Design Patterns: Elements of Reusable Object-Oriented Software]]'': "Define an interface for creating an object, but let subclasses decide which class to instantiate.
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
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 |url=http://shop.oreilly.com/product/9780596007126.do |title=Head First Design Patterns: A Brain-Friendly Guide |last2=Robson |first2=Elisabeth |last3=Sierra |first3=Kathy |last4=Bates |first4=Bert |publisher=O'Reilly Media |year=2004 |isbn=978-0-596-00712-6 |editor-last1=Hendrickson |editor-first1=Mike |edition=1st |volume=1 |page=162 |format=paperback |access-date=2012-09-12 |editor-last2=Loukides |editor-first2=Mike}}</ref>
The pattern can also rely on the implementation of an [[Interface (object-oriented programming)|interface]].
==Structure==
Line 56 ⟶ 24:
<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 the exact concrete class that 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++
<syntaxhighlight lang="c++">
import std;
enum class ProductId {MINE, YOURS};
// defines the interface of objects the factory method creates.
Line 81 ⟶ 44:
public:
void print() {
std::
}
};
Line 89 ⟶ 52:
public:
void print() {
std::
}
};
Line 129 ⟶ 92:
[[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.
=== Example implementations ===
Line 182 ⟶ 145:
}
</syntaxhighlight>
A factory method is just an addition to the <code>PersonFactory</code> class. It creates the object of the class through interfaces but
<syntaxhighlight lang="csharp">
Line 225 ⟶ 188:
{
IProduct product = new Phone();
// Do something with the object after
product.SetPrice(20.30);
return product;
Line 231 ⟶ 194:
}
</syntaxhighlight>
====[[Java (programming language)|Java]]====
Line 238 ⟶ 201:
[[File:Maze game UML.svg]]
The <code>MazeGame</code> uses
<syntaxhighlight lang="java">
Line 268 ⟶ 231:
</syntaxhighlight>
<syntaxhighlight lang="java">
Line 290 ⟶ 253:
====[[PHP]]====
<syntaxhighlight lang="php">
Line 331 ⟶ 294:
==== [[Python (programming language)|Python]] ====
<syntaxhighlight lang="python3">
Line 395 ⟶ 358:
==Uses==
* In [[ADO.NET]], [https://docs.microsoft.com/en-us/dotnet/api/system.data.idbcommand.createparameter?view=netframework-4.8 IDbCommand.CreateParameter] is an example of the use of factory method to connect parallel class hierarchies.
* In [[Qt (toolkit)|Qt]], [http://qt-project.org/doc/qt-5.0/qtwidgets/qmainwindow.html#createPopupMenu QMainWindow::createPopupMenu] {{Webarchive|url=https://web.archive.org/web/20150719014739/http://qt-project.org/doc/qt-5.0/qtwidgets/qmainwindow.html#createPopupMenu |date=2015-07-19 }} is a factory method declared in a framework that can be overridden in [[application code]].
* In [[Java (programming language)|Java]], several factories are used in the [http://download.oracle.com/javase/1.5.0/docs/api/javax/xml/parsers/package-summary.html javax.xml.parsers] package
* 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==
Line 405 ⟶ 368:
* [[Builder pattern]], another creational pattern
* [[Template method pattern]], which may call factory methods
* [[Joshua Bloch]]'s idea of a
==Notes==
{{Reflist}}▼
==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
Line 416 ⟶ 380:
| isbn = 0-201-48567-2
| author1-link=Martin Fowler (software engineer) }}
*{{cite book |author1=Cox, Brad J. |
title=Object-oriented programming: an evolutionary approach |
Line 450 ⟶ 408:
| 2 = Factory method examples
}}
* [http://designpattern.co.il/Factory.html Factory Design Pattern] {{Webarchive|url=https://web.archive.org/web/20180110103637/http://designpattern.co.il/Factory.html |date=2018-01-10 }} Implementation in Java
* [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)
|