Factory method pattern
The term factory method (English factory method) called a design pattern in the field of software development. The pattern describes how an object is created by calling a method rather than by directly calling a constructor. It thus belongs to the category of generation pattern.
However, the term factory method can be misleading insofar as he referred to, depending on the speaker both two slightly different design patterns as well as the method itself.
Term meaning according to Gang of Four
The pattern is one of the so-called GoF design patterns ( Gang of Four, see Gang of Four ). It refers to a pattern in which the interface for creating an object is an ( abstract ) method of a superclass. However, the concrete implementation of the creation of new objects does not take place in the upper class, but derived from their subclasses that implement the abstract method said.
The pattern thus describes the generation of product objects whose concrete type is a subtype of an abstract class of product, which is determined by subclasses of a producer class. It is sometimes called "virtual constructor " (virtual constructor ) called.
Demarcation
The term " factory method " is also often used simply in practice only for a static method that creates a new object, like a constructor.
Example: Instead of
Some Object o = new SomeObject (); is written using the colloquially known as " factory method " static method:
Some Object o = SomeObjectFactory.createNewInstance (); In this case, no use of lower grade or polymorphism is provided.
However, this use of the term factory method is not correct in the sense of the Gang of Four.
Use
The factory method ( in the GoF meaning) is used when a class to be generated from it objects can not know, or should, or if your subclasses to determine which objects are created. Typical applications are frameworks and class libraries. In GRASP, the factory method is a solution to approach the objectives of low coupling and high cohesion.
UML diagram
The following class diagram shows the four roles involved in the design pattern. KonkreterErzeuger inherits the abstract factory method of producer and implemented it so that it produces ConcreteProduct, which in turn implements product.
Actors
The product is the base type ( class or interface ) for the product to be produced. Declared by the producer, the factory method to produce such a product and can include a default implementation. Sometimes an implementation is given for the factory method, which generates a " standard product ".
ConcreteProduct implements the Product interface. ( It is a concrete subtype of product). KonkreterErzeuger overrides the factory method to generate its corresponding concrete products (for example, by calling the constructor of a concrete product class).
Benefits
Factory methods decouple their callers of implementations of specific product classes. This is particularly valuable when frameworks evolve during the lifetime of an application - other classes as may be created at a later time instances, without the application has to change.
Many object-oriented programming languages dictate the name of the constructor. In contrast, a factory method ( in the broader sense of the term ) have a more meaningful name, and there may be several factory methods different name and different semantics. For example, a method Color.createFromRGB () form a color object from RGB values , a method Color.createFromHSV (), a color object from HSV values.
Disadvantages
The use of this pattern generation amounts to subclassing. There must be a class that can hold the class method for generating.
Examples
Virtual Method in interface or class
The virtual method can be defined (for example, front) in both the interface and the interface or in the class, which is otherwise responsible for objects of a type. Subclasses can then generate specific types. Typical scenarios:
Creating Dependent Objects
Examples:
- Java: java.sql.Connection.createStatement () - the statement generated refers to the Connection, and "lives in this".
- . NET: System.Data.IDbConnection.CreateCommand () - the IDbCommand generated refers to the Connection, and "lives in this".
Often the generated dependent objects are deactivated for factory methods which depend on objects, such as IDbCommand has a method CreateParameter (). Therefore, classes can be with such factory methods rather than " factory classes " (with primary responsibility " Object Creation" ) understand - in contrast to the abstract factory.
Generating independent objects via centralized " indexed constructors "
One method from a family of factory methods is called with the help of a dictionary for a key. Code snippet ( with C # delegates instead of subclasses - the delegate type represents the producers, each concrete anonymous method each one Concretely, producer ):
Delegate IFoo CreateFoo ( IContext creation parameter);
static IDictionary
IFoo newObject = FooFactory [key] (some context); Allows a compact, descriptive design of object creation. Danger (especially if - referenced in the Dictionary directly to function calls - for example, in C #) that the factory objects take on more responsibility.
"Static Factory Method "
One static method that returns an object of a type or subtype. No "virtual constructor " - meaning the matter: Central, class-based access point for object creation similar to new. Sometimes requires the introduction of a single class only as " Factory Method Holder ". Examples:
- Java: java.util.Collections.singletonMap ()
- Java: javax.xml.parsers.DocumentBuilderFactory.newInstance ()
An example of C
The following example illustrates the use of the GoF pattern in a fictional framework for a restaurant software.
Restaurant ( producer ) delivers meals ( products). The basic method for providing a meal is always the same: Take an order, Preparing meal, serve meal. This can be everything already in the class to implement restaurant on the preparation of the meal. The preparation (generation ) is dependent on the type of restaurant: A pizzeria ( concrete producer ) produces pizzas ( concrete products ), a sausage stand rust causing rust sausages.
The class implements this restaurant a method liefereMahlzeit (), which returns the meal, including the ordering and Serviervorgangs. It uses an abstract method bereiteMahlzeitZu (), which prepared the concrete for the concrete restaurant meal ( produced ). bereiteMahlzeitZu () is the factory - method and must be implemented according to each specific restaurant.
It is easy to see how this framework can be used for a new type of restaurant, with a meal Offer: From meal and restaurant each a new class must be derived, where the derived Restaurant class in its method bereiteMahlzeitZu () in this Restaurant offered food cook ( produce ) must.
# Include
An abstract Factory ( Abstract Factory ) is realized in general by means of factory methods.
From template methods ( Template Method ) out factory methods are typically called.
Factory methods are often found in singletons.