Monday, July 30, 2007

Abstract Factory



The Abstract Factory is intended to provide a single interface for clients to use when they need to create a family of related objects without having to specify concrete classes. For example, imagine a system that needs to implement platform-specific user interface objects (menus, toolbars, and so forth) for several different platforms. Abstract Factory simplifies the interface for the client by localizing all of the initialization strategies within a single class, the Abstract Factory. The pattern works to ensure that all the strategies can work together correctly.

To understand Abstract Factory, examine the class diagram shown in the above figure. Note that there are two separate hierarchies. The first represents the various abstractions the client has interest in. For each abstraction, there exists an abstract class definition (AbstractClass1, AbstractClass2, and so on), and the subclasses that provide the concrete implementations (ConcreteClass1A, ConcreteClass1B, and so forth). In the second hierarchy, an abstract AbstractFactory class is defined to provide the interface for each class that is responsible for creating the members of a particular family. For example, ConcreteFactoryB is responsible for creating objects from classes ConcreteClass1B, ConcreteClass2B, and the like.

All the client needs to worry about is which family of objects it is interested in creating and calling the appropriate Factory method. Because the client only knows about the abstract interface, it can interact with objects from different families without having to know about their internal implementation details. This approach has the benefit that the family can be allowed to grow without the need to modify the client.

The primary drawback of AbstractFactory is that it can limit your flexibility when you want to take advantage of a specific capability a family may have. This is because you must provide a common interface across all families, even if some of them do not have this capability.

Example:


Participants

The classes and/or objects participating in this pattern are:
  • AbstractFactory (ContinentFactory) - declares an interface for operations that create abstract products
  • ConcreteFactory (AfricaFactory, AmericaFactory) - implements the operations to create concrete product objects
  • AbstractProduct (Herbivore, Carnivore) - declares an interface for a type of product object
  • Product (Wildebeest, Lion, Bison, Wolf) - defines a product object to be created by the corresponding concrete factory & implements the AbstractProduct interface
  • Client (AnimalWorld) - uses interfaces declared by AbstractFactory and AbstractProduct classes


No comments: