 The factory method pattern is an object-oriented design pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. The factory method design pattern handles this problem by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created. More generally, the term factory method is often used to refer to any method whose main purpose is creation of objects. Image File history File links FactoryMethod. ...
Object-oriented programming (OOP) is a computer programming paradigm in which a software system is modeled as a set of objects that interact with each other. ...
In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. ...
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. ...
In strictly mathematical branches of computer science the term object is used in a purely mathematical sense to refer to any thing. While this interpretation is useful in the discussion of abstract theory, it is not concrete enough to serve as a primitive datatype in the discussion of more concrete...
In object-oriented programming, a class is a programming language construct used to group related fields and methods. ...
In object-oriented programming, the term method refers to a subroutine that is exclusively associated either with a class (called class methods, static methods, or factory methods) or with an object (called instance methods). ...
In object-oriented programming, a subclass is a class that inherits some properties from its superclass. ...
A derived type is a type given a new type but structurally the same as the original type. ...
Common usage
- Factory methods are common in toolkits and frameworks where library code needs to create objects of types which may be subclassed by applications using the framework.
- Parallel class hierarchies often require objects from one hierarchy to be able to create appropriate objects from another.
In computer programming, widget toolkits (or GUI toolkits) are sets of basic building elements for graphical user interfaces. ...
Look up Framework in Wiktionary, the free dictionary. ...
Other benefits and variants Although the motivation behind the factory method pattern is to allow subclasses to choose which type of object to create, there are other benefits to using factory methods, many of which do not depend on subclassing. Therefore, it is common to define "factory methods" that are not polymorphic to create objects in order to gain these other benefits. Such methods are often static.
Descriptive names A factory method has a distinct name. In many object-oriented languages, constructors must have the same name as the class they are in, which can lead to ambiguity if there is more than one way to create an object (see overloading). Factory methods have no such constraint and can have descriptive names. As an example[dubious – discuss], when complex numbers are created from two real numbers the real numbers can be interpreted as Cartesian or polar coordinates, but using factory methods, the meaning is clear (the following examples are in Java): In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special block of statements called when an object is created, either when it is declared (statically constructed on the stack, possible in C++ but not in Java and other object-oriented languages) or dynamically constructed...
In computer science, polymorphism means allowing a single definition to be used with different types of data (specifically, different classes of objects). ...
In mathematics, a complex number is a number which is often formally defined to consist of an ordered pair of real numbers , often written: In mathematics, the adjective complex means that the underlying number field is complex numbers, for example complex analysis, complex matrix, complex polynomial and complex Lie algebra. ...
Java language redirects here. ...
class Complex { public static Complex fromCartesian(double real, double imag) { return new Complex(real, imag); } public static Complex fromPolar(double modulus, double angle) { return new Complex(modulus * cos(angle), modulus * sin(angle)); } private Complex(double a, double b) { //... } } Complex c = Complex.fromPolar(1, pi); // Same as fromCartesian(-1, 0) When factory methods are used for disambiguation like this, the constructor is often made private to force clients to use the factory methods.
Encapsulation Factory methods encapsulate the creation of objects. This can be useful if the creation process is very complex, for example if it depends on settings in configuration files or on user input. Consider as an example a program to read image files and make thumbnails out of them. The program supports different image formats, represented by a reader class for each format: See also Category:Graphics file formats Here is a summary of the most common graphics file formats: Some file formats, e. ...
The ThumbsPlus image file manager showing folder tree in the upper left and 12 thumbnail-size images to the right. ...
public interface ImageReader { public DecodedImage getDecodedImage(); } public class GifReader implements ImageReader { public GifReader( InputStream in ) { // check that it's a gif, throw exception if it's not, then if it is // decode it. } public DecodedImage getDecodedImage() { return decodedImage; } } public class JpegReader implements ImageReader { //.... } Each time the program reads an image it needs to create a reader of the appropriate type based on some information in the file. This logic can be encapsulated in a factory method: public class ImageReaderFactory { public static ImageReader getImageReader( InputStream is ) { int imageType = figureOutImageType( is ); switch( imageType ) { case ImageReaderFactory.GIF: return new GifReader( is ); case ImageReaderFactory.JPEG: return new JpegReader( is ); // etc. } } } The code fragment in the previous example uses a switch statement to associate an imageType with a specific factory object. Alternatively, this association could also be implemented as a mapping. This would obviate the switch statement and instead replace it with a lookup in an associative array. In computer programming, a switch statement is a type of control statement that exists in most modern imperative programming languages (e. ...
In object-oriented computer programming, a factory object is an object for creating other objects. ...
In computing, an associative array, also known as a map, lookup table, or dictionary, is an abstract data type very closely related to the mathematical concept of a function with a finite domain. ...
An associative array (also map, hash, dictionary, finite map, lookup table, and in query-processing an index or index file) is an abstract data type composed of a collection of keys and a collection of values, where each key is associated with one value. ...
Limitations There are three limitations associated with the use of the factory method. The first relates to refactoring existing code; the other two relate to inheritance. A code refactoring is any change to a computer program which improves its readability or simplifies its structure without changing its results. ...
- The first limitation is that refactoring an existing class to use factories breaks existing clients. For example, if class Complex was a standard class, it might have numerous clients with code like:
Complex c = new Complex(-1, 0); - Once we realize that two different factories are needed, we change the class (to the code shown above). But since the constructor is now private, the existing client code no longer compiles.
- The second limitation is that, since the pattern relies on using a private constructor, the class cannot be extended. Any subclass must invoke the inherited constructor, but this cannot be done if that constructor is private.
- The third limitation is that, if we do extend the class (e.g., by making the constructor protected -- this is risky but possible), the subclass must provide its own re-implementation of all factory methods with exactly the same signatures. For example, if class StrangeComplex extends Complex, then unless StrangeComplex provides its own version of all factory methods, the call StrangeComplex.fromPolar(1, pi) will yield an instance of Complex (the superclass) rather than the expected instance of the subclass.
All three problems could be alleviated by altering the underlying programming language to make factories first-class class members (rather than using the design pattern).
Uses - In ADO.NET, IDbCommand.CreateParameter is an example of the use of factory method to connect parallel class hierarchies.
- In Qt, QMainWindow::createPopupMenu is a factory method declared in a framework which can be overridden in application code.
- In Java (programming language) several factories are used in the javax.xml.parsers package. e.g. javax.xml.parsers.DocumentBuilderFactory or javax.xml.parsers.SAXParserFactory.
ADO.NET is a set of computer software components that can be used by programmers to access data and data services. ...
For other uses, see Qt. ...
Java language redirects here. ...
See also A software design pattern, the Abstract Factory Pattern provides a way to encapsulate a group of individual factories that have a common theme. ...
The Builder Pattern is a software design pattern. ...
Template method: UML class diagram. ...
In object-oriented computer programming, a factory object is an object for creating other objects. ...
References Martin Fowler is a famous author and international speaker on software architecture, specializing in object-oriented analysis and design, UML, Patterns, and agile software development methodologies, including Extreme Programming. ...
Erich Gamma is a co-author of the influential computer science textbook, Design Patterns. ...
Richard Helm is currently with The Boston Consulting Group where he consults on the strategic application of technology to business. ...
This article is about the book by Gamma et al. ...
PDF is an abbreviation with several meanings: Portable Document Format Post-doctoral fellowship Probability density function There also is an electronic design automation company named PDF Solutions. ...
Bertrand Meyer (born 1950 in France) developed the Eiffel programming language, and is an author, academic and consultant in the field of computer languages. ...
Year 2007 (MMVII) is the current year, a common year starting on Monday of the Gregorian calendar and the AD/CE era in the 21st century. ...
is the 71st day of the year (72nd in leap years) in the Gregorian calendar. ...
External links - Implementing the factory method pattern in PHP
- A formal definition of Factory method pattern in LePUS 2 (a formal language for defining design patterns)
- Description from the Portland Pattern Repository
- Description from the KhoaNgo blog
- Factory Method pattern discussion with 1-page examples (C++ and Java)
- Jt J2EE Pattern Oriented Framework
| Design patterns in Design Patterns | Creational: Abstract factory • Builder • Factory • Prototype • Singleton In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. ...
This article is about the book by Gamma et al. ...
A software design pattern, the Abstract Factory Pattern provides a way to encapsulate a group of individual factories that have a common theme. ...
The Builder Pattern is a software design pattern. ...
A prototype pattern is a creational design pattern used in software development when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. ...
In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. ...
Structural: Adapter • Bridge • Composite • Decorator • Façade • Flyweight • Proxy In computer programming, the adapter design pattern (often referred to as the wrapper pattern or simply a wrapper) adapts one interface for a class into one that a client expects. ...
The bridge pattern is a design pattern used in software engineering which is meant to decouple an abstraction from its implementation so that the two can vary independently (Gamma et. ...
This does not cite any references or sources. ...
For decorators in Python, see Python syntax and semantics#Decorators. ...
The façade pattern is an object-oriented design pattern. ...
Flyweight is a software design pattern. ...
// In computer programming, the proxy pattern is a software design pattern. ...
Behavioral: Chain of responsibility • Command • Interpreter • Iterator • Mediator • Memento • Observer • State • Strategy • Template method • Visitor In Object Oriented Design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. ...
In object-oriented programming, the Command pattern is a design pattern in which objects are used to represent actions. ...
In computer programming, the interpreter pattern is a particular design pattern. ...
In object-oriented programming, an iterator is an object allowing one to sequence through all of the elements or parts contained in some other object, typically a container or list. ...
The mediator pattern is a software design pattern that provides a unified interface to a set of interfaces in a subsystem. ...
The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo by rollback). ...
The observer pattern (sometimes known as publish/subscribe) is a design pattern used in computer programming to observe the state of an object in a program. ...
A behavioral software design pattern, state pattern is used in computer programming to represent the state of an object. ...
In computer programming, the strategy pattern is a particular software design pattern, whereby algorithms can be selected on-the-fly at runtime. ...
Template method: UML class diagram. ...
In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure. ...
| |