Content deleted Content added
No edit summary Tags: Reverted Mobile edit Mobile web edit |
Squeakachu (talk | contribs) m Rollback edit(s) by 2806:261:40B:1E77:D96E:9F27:B4D4:4C1F (talk): non-constructive (RW 16.1) |
||
Line 1:
{{Short description|Object-oriented software design pattern}}
==Overview==
Line 8:
The Factory Method design pattern solves problems like:
<ref>{{cite web
* 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
| 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>
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 209 ⟶ 231:
}
</syntaxhighlight>
You can see we have used <code>
====[[Java (programming language)|Java]]====
Line 427 ⟶ 449:
| 1 = Computer Science Design Patterns
| 2 = Factory method examples
}}
* [
* [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)
|