Applied Design Patterns with Java
Creational :: Abstract Factory (87) {C ch 5}
Implementation
Here are some useful techniques for implementing the Abstract Factory pattern.
- Factories as singletons.
An application typically needs only one instance of a ConcreteFactory per product family. So it's usually best
implemented as a Singleton (127).
- Creating the products.
AbstractFactory only declares an interface for creating products. It's up to ConcreteProduct subclasses to actually
create them. The most common way to do this is to define a factory method (see Factory Method (107)) for each product. A concrete factory
will specify its products by overriding the factory method for each. While this implementation is simple, it requires
a new concrete factory subclass for each product family, even if the product families differ only slightly. If
many product families are possible, the concrete factory can be implemented using the Prototype (117) pattern. The concrete factory
is initialized with a prototypical instance of each product in the family, and it creates a new product by cloning
its prototype. The Prototype-based approach eliminates the need for a new concrete factory class for each new product
family.
- Defining extensible factories.
AbstractFactory usually defines a different operation for each kind of product it can produce. The kinds of products
are encoded in the operation signatures. Adding a new kind of product requires changing the AbstractFactory interface
and all the classes that depend on it. A more flexible but less safe design is to add a parameter to operations
that create objects. This parameter specifies the kind of object to be created. It could be a class identifier,
an integer, a string, or anything else that identifies the kind of product. In fact with this approach, AbstractFactory
only needs a single "Make" operation with a parameter indicating the kind of object to create.
Related Patterns
AbstractFactory classes are often implemented with
factory methods (Factory Method (107)), but they can also be implemented using Prototype
(117).
A
concrete factory is often a singleton (Singleton
(127)).
Catalog Creational Prev Next