Computer Science Design Patterns/Abstract Factory

{{Computer Science Design Patterns/Page The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes. In normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part of the theme. The client does not know (or care) which concrete objects it gets from each of these internal factories, since it uses only the generic interfaces of their products. This pattern separates the details of implementation of a set of objects from their general usage and relies on object composition, as object creation is implemented in methods exposed in the factory interface. An example of this would be an abstract factory class  that provides interfaces to create a number of products (e.g.   and  ). The system would have any number of derived concrete versions of the  class like   or , each with a different implementation of   and   that would create a corresponding object like   or. Each of these products is derived from a simple abstract class like  or   of which the client is aware. The client code would get an appropriate instance of the  and call its factory methods. Each of the resulting objects would be created from the same  implementation and would share a common theme (they would all be fancy or modern objects). The client would only need to know how to handle the abstract  or   class, not the specific version that it got from the concrete factory.
 * mode={{{mode|reading}}}
 * page={{{page|{{SUBPAGENAME}}}}}
 * stage=25
 * stage date=21 Jun, 2013
 * stage comment=Add more examples of use.
 * previous=
 * next=Adapter
 * content=

A factory is the location of a concrete class in the code at which objects are constructed. The intent in employing the pattern is to insulate the creation of objects from their usage and to create families of related objects without having to depend on their concrete classes. This allows for new derived types to be introduced with no change to the code that uses the base class.

Use of this pattern makes it possible to interchange concrete implementations without changing the code that uses them, even at runtime. However, employment of this pattern, as with similar design patterns, may result in unnecessary complexity and extra work in the initial writing of code. Additionally, higher levels of separation and abstraction can result in systems which are more difficult to debug and maintain. Therefore, as in all software designs, the trade-offs must be carefully evaluated.

Definition
The essence of the Abstract Factory Pattern is to "Provide an interface for creating families of related or dependent objects without specifying their concrete classes".

Usage
The factory determines the actual concrete type of object to be created, and it is here that the object is actually created (in C++, for instance, by the  operator). However, the factory only returns an abstract pointer to the created concrete object.

This insulates client code from object creation by having clients ask a factory object to create an object of the desired abstract type and to return an abstract pointer to the object.

As the factory only returns an abstract pointer, the client code (that requested the object from the factory) does not know &mdash; and is not burdened by &mdash; the actual concrete type of the object that was just created. However, the type of a concrete object (and hence a concrete factory) is known by the abstract factory; for instance, the factory may read it from a configuration file. The client has no need to specify the type, since it has already been specified in the configuration file. In particular, this means:


 * The client code has no knowledge whatsoever of the concrete type, not needing to include any header files or class declarations related to it. The client code deals only with the abstract type. Objects of a concrete type are indeed created by the factory, but the client code accesses such objects only through their abstract interface.
 * Adding new concrete types is done by modifying the client code to use a different factory, a modification that is typically one line in one file. The different factory then creates objects of a different concrete type, but still returns a pointer of the same abstract type as before &mdash; thus insulating the client code from change. This is significantly easier than modifying the client code to instantiate a new type, which would require changing every location in the code where a new object is created (as well as making sure that all such code locations also have knowledge of the new concrete type, by including for instance a concrete class header file). If all factory objects are stored globally in a singleton object, and all client code goes through the singleton to access the proper factory for object creation, then changing factories is as easy as changing the singleton object.

Class diagram


The method  on the   interface returns objects of type. What implementation of  is returned depends on which implementation of   is handling the method call.

Note that, for conciseness, this class diagram only shows the class relations for creating one type of object.

Implementations
The output should be either "I'm a WinButton" or "I'm an OSXButton" depending on which kind of factory was used. Note that the Application has no idea what kind of GUIFactory it is given or even what kind of Button that factory creates.

This abstract factory creates books.

This abstract factory creates musicians.

Alternative implementation using the classes themselves as factories:

Another example:

}}