Factory method pattern: Difference between revisions

Content deleted Content added
m Definition: Literal quote now matches the source.
Examples: fixed weasel word
 
(6 intermediate revisions by 4 users not shown)
Line 1:
{{Short description|Object-oriented software design pattern}}
In [[object-oriented programming]], the '''factory method pattern''' is a [[software design pattern|design pattern]] that uses factory methods to deal with the problem of [[object creation|creating objects]] without having to specify their exact [[class (computer programming)|classes]]. Rather than by calling a [[Constructor (object-oriented programming)|constructor]], this is accomplished by invoking a factory method to create an object. Factory methods can be specified in an [[Interface (object-oriented programming)|interface]] and implemented by subclasses or implemented in a base class and optionally [[method overriding|overridden]] by subclasses. It is one of the 23 classic design patterns described in the book ''[[Design Patterns]]'' (often referred to as the "Gang of Four" or simply "GoF") and is subcategorized as a [[creational pattern]].{{sfn|Gamma|Helm|Johnson|Vlissides|19941995|page=107}}
 
==Overview==
Line 10:
 
==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. Factory method lets a class defer instantiation to subclasses."<ref name="gof">{{cite book |last1=Gamma |first1=Erich |last2author1-link=Helm |first2=Richard |last3=Johnson|first3=Ralph |last4=Vlissides |first4=JohnErich Gamma |title=Design Patterns: Elements of Reusable Object-Oriented Software |publishertitle-link=Addison-WesleyDesign |year=1994Patterns |isbnlast2=0-201-63361-2Helm |author1-linkfirst2=Erich GammaRichard |author2-link=Richard Helm |last3=Johnson |first3=Ralph |author3-link=Ralph Johnson (computer scientist) |last4=Vlissides |first4=John |author4-link=John Vlissides |titlepublisher=Addison-linkWesley |year=Design Patterns1995 |isbn=0-201-63361-2}}</ref>
 
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 inaccessible to the composing object, may not provide a sufficient level of abstraction or may otherwise not be included in 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 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 |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>
| 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 = 1
| page = 162
| publisher = O'REILLY
| format = paperback
| isbn = 978-0-596-00712-6
| access-date = 2012-09-12
| url = http://shop.oreilly.com/product/9780596007126.do
}}</ref>
The pattern can also rely on the implementation of an [[Interface (object-oriented programming)|interface]].
 
Line 49 ⟶ 27:
 
== Examples ==
This [[C++1423]] implementation is based on the pre C++98 implementation in the ''[[Design Patterns]]'' book.{{sfn|Gamma|Helm|Johnson|Vlissides|19941995|page=122}}{{Which one|date=August 2024}}
<syntaxhighlight lang="c++">
import std;
#include <iostream>
#include <memory>
 
enum class ProductId {MINE, YOURS};
 
// defines the interface of objects the factory method creates.
Line 67 ⟶ 44:
public:
void print() {
std::cout << println("this=" << this << "{} print MINE\n", this);
}
};
Line 75 ⟶ 52:
public:
void print() {
std::cout << println("this=" << this << "{} print YOURS\n", this);
}
};