- 1. The reasons
- 2. Problems
- 3. Discussion
- 4. Structure
- 5. Example
- 6. Control List
- 7. Rules of thumb
The reasons
- Providing an interface for creating families of related or dependent objects without specifying their specific classes.
- Hierarchy that encapsulates: many possible "platforms" and building a set of "products".
- The new operator is considered harmful.
Problems
If an application is to be portable, it needs to encapsulate all dependencies of the target platform. These "platforms" can include: windowing system, operating system, database, etc. All too often this encapsulation is not designed up front, and many case statements or various #ifdef with options for all currently supported platforms are starting to breed like rabbits all over the code.
Discussion
Providing an abstraction layer that abstracts the creation of families of related or dependent objects without explicitly specifying their concrete classes. The factory object is responsible for providing services to create the entire family of classes on all supported platforms. Clients never create platform objects directly, they ask the factory to do it for them.
This mechanism makes it easy to change the family of derived class objects, since a particular factory object class only appears once in the application, where it is created. An application can provide the ability to modify an entire family of derived objects by simply instantiating another specific instance of the abstract factory.
Because the service provided by the factory object is so common, it is usually implemented as a Singleton. Or it can be implemented using static class methods in the case of the C++ programming language.
Structure
An abstract factory defines a factory method for each generated object. Each factory method encapsulates the new operator and concrete, platform-specific classes. Each "platform" is then modeled by a derived Factory class.
Example
The purpose of Abstract Factory is to provide an interface for creating families of related objects without specifying concrete classes. This model can be found in sheet metal stamping equipment used in the manufacture of automobiles. Stamping equipment is an abstract factory that creates auto parts. The same mechanism is used to stamp right side doors, left doors, right front fenders, left front fenders, hoods, etc. for different car models. Through the use of rollers to change stamping patterns, the specific grades produced by the machine can be changed within minutes.
Control List
- Decide if "platform independence" and authoring services are causing problems in your code.
- Derive a matrix of "platforms" and "products". For what platform, what classes are required.
- Define a factory interface, which consists of a factory method for each spawned object.
- Define a production class for each platform that encapsulates all references to the new operator.
- The client must remove all references to new and use factory methods to create child objects.
Rules of thumb
- Sometimes parent templates compete: there are times when either the Prototype template or the Abstract Factory template can be used with success. Otherwise, they complement each other: An Abstract Factory can store a set of prototypes from which objects can be cloned and returned. The Builder may use one of the other templates to implement the components to be created. Abstract Factory, Builder and Prototype can use Singleton in their implementation.
- Abstract Factory, Builder and Prototype define a factory object that is responsible for creating a class of generated objects and makes it a system parameter. An abstract factory has a factory object or method that produces objects of multiple classes. The Builder has a factory object that builds a complex product incrementally using an appropriate complex protocol. A prototype has a factory object (aka prototype) that creates a product by copying the prototype object.
- Abstract Factory classes are often implemented using Factory Methods, but they can also be implemented using Prototype.
- Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.
- Builder focuses on building a complex object step by step. The abstract factory emphasizes a family of product objects (simple or complex). The Builder returns the product as the last step, but as far as the abstract factory is concerned, the product returns immediately.
- Often projects start with Factory Method (less complex, customizable, subclasses proliferate) and evolve towards Abstract Factory, Prototype, or Builder (more flexible, more complex) as the developer finds that more flexibility is required when creating an object.