Intro to Design Patterns: Abstract Factory Pattern
Join the DZone community and get the full member experience.Join For Free
Andre Mare's series on "Gang of Four" design patterns continues. First, he covered the Builder Pattern, next the Factory Method Pattern. Today he continues with the "Abstract Factory Pattern". Read on for all the details, diagrams, and sample code! -- Geertjan Wielenga, JavaLobby Zone Leader
Provide an interface for creating families of related or dependent objects without specifying their concrete classes. - Gof
The Abstract Factory Pattern defines an interface with a set of methods to create a family of related products. The family of related objects is defined through a set of product types. The implementation of the product types is delegated to a set of concrete product subclasses. The creation of the concrete product classes is implemented by series of concrete factory classes. The Abstract Factory Pattern defers the creation of the concrete products to the concrete factory classes that implements the abstract factory. The client object is decoupled from the concrete product classes and the concrete factory classes through the Abstract Factory interface. The core of the Abstract Factory Pattern is to create a group of related objects that might have different implementations. The system is therefore independent of the implementation of the product types. The Abstract Factory Pattern also enables the system to replace a set of related product classes with another set, by changing the concrete factory class.
The Abstract Factory Pattern consists of an AbstractFactory, ConcreteFactory, AbstractProduct, ConcreteProduct and Client.
- The AbstractFactory defines a factory type that list operations for creating the set of related abstract product types.
- The ConcreteFactory implements the list of operations and create the concrete product objects that are associated with the specific concrete factory class.
- The AbstractProduct declares the operations available for the specific product type. The implementation of the product type is delegated to the subclasses of the product type.
- The ConcreteProduct is created by a specific concrete factory object and is the realization of a specific product type. The client system is decoupled from the actual concrete product class, but invokes the implementation through the abstract product.
- The Client object is decoupled from the ConcreteFactory and ConcreteProduct objects and work with the interfaces declared by the AbstractFactory and AbstractProduct types.
The Abstract Factory Pattern can be implemented using the Factory Method Pattern, Prototype Pattern or the Singleton Pattern. The ConcreteFactory object can be implemented as a Singleton as only one instance of the ConcreteFactory object is needed.
Java Sample Code
Download: Greek Salad Instruction
The following example illustrates the use of the Abstract Factory pattern. The Greek Salad Instruction example illustrates the creation of a family of related objects that supply the instructions for making different types of Greek Salads.
The example consists of the following classes:
- SaladInstructionsKit.java - (AbstractFactory)
- DicedGreekSaladInstructionFactory.java - (ConcreteFactory)
- SlicedGreekSaladInstructionFactory.java - (ConcreteFactory)
- CucumberInstructions.java - (AbstractProduct)
- TomatoInstructions.java - (AbstractProduct)
- SlicedTomatoInstructions.java - (ConcreteProduct)
- DicedCucumberInstructions.java - (ConcreteProduct)
- DicedTomatoInstructions.java - (ConcreteProduct)
- SlicedCucumberInstructions.java - (ConcreteProduct)
- GreekSaladInstructionsClient.java - (Client)
- MainClass.java - (class contains main method)
The GreekSaladInstructionsClient class makes use of the AbstractFactory and AbstractProduct classes to determine the instructions on making a Greek Salad. Depending on the ConcreteFactory, the salad may be diced or sliced. The class will invoke the appropriate methods on the concrete product classes through the abstract product types.
The SaladInstructionsKit defines a factory type that list operations for creating the set of related abstract product types. The Abstract Factory defers the creation of the concrete product classes to the concrete factory classes. Each specific concrete factory class will create a specific concrete product class.
The TomatoInstructions declares the operations available for the specific product type. The implementation of the product type is delegated to the subclasses of the product type.
The CucumberInstructions declares the operations available for the specific product type. The implementation of the product type is delegated to the subclasses of the product type.
The SlicedGreekSaladInstructionFactory implements the list of operations and create the concrete product objects that is associated with the specific concrete factory class. The concrete factory creates specific concrete product classes to create a sliced Greek Salad.
The SlicedCucumberInstructions is created by a specific concrete factory object and is the realization of a specific product type.
The SlicedTomatoInstructions is created by a specific concrete factory object and is the realization of a specific product type.
Sequence Diagram by "Yanic Inghelbrecht" with Trace Modeler
Abstract Factory vs. Factory Method
The methods of an Abstract Factory are implemented as Factory Methods. Both the Abstract Factory Pattern and the Factory Method Pattern decouples the client system from the actual implementation classes through the abstract types and factories. The Factory Method creates objects through inheritance where the Abstract Factory creates objects through composition.
- Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley, 1995
Published at DZone with permission of Andre Mare. See the original article here.
Opinions expressed by DZone contributors are their own.