Content deleted Content added
No edit summary Tags: Mobile edit Mobile web edit |
No edit summary Tags: Reverted Mobile edit Mobile web edit |
||
Line 1:
{{Short description|Object-oriented software design pattern}}
[[Instagram (Dimitri Vegas & Like Mike, David Guetta, Daddy Yankee, Afro Bros, and Natti Natasha song)|'''In''']] [[object oriented 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==
Line 8:
The Factory Method design pattern solves problems like:
<ref>{{cite web |last=self.rooms |title=The Factory Method design pattern - Problem, Solution, and Applicability |url=http://w3sdesign.com/?gr=c03&ugr=proble
* How can an object be created so that subclasses can redefine which class to instantiate?
* How can a class defer instantiation to subclasses?
Line 24:
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 |url=http://shop.oreilly.com/product/9780596007126.do |title=Head First Design Patterns |last2=Freeman |first2=Elisabeth |last3=Kathy |first3=Sierra |last4=Bert |first4=Bates |publisher=O'REILLY |year=2004 |isbn=978-0-596-00712-6 |editor-last1=Hendrickson |editor-first1=Mike |volume=1 |page=162 |pages=2024 |language=Español |format=paperback |access-date=2012-09-12 |editor-last2=Loukides |editor-first2=Mike}}</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.
Line 231 ⟶ 209:
}
</syntaxhighlight>
You can see we have used <code>
====[[Java (programming language)|Java]]====
Line 449 ⟶ 427:
| 1 = Computer Science Design Patterns
| 2 = Factory method examples
}}<ref>{{Cite book |title=Design patterns: elements of reusable object-oriented software |date=2011 |publisher=Addison-Wesley |isbn=978-0-201-63361-0 |editor-last=Gamma |editor-first=Erich |edition=39. printing |series=Addison-Wesley professional computing series |___location=Boston, Mass. Munich}}</ref>
* [
* [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)
|