Abstract Factory

Also called a kit.

Problem

A client uses a collection of related products or components, but can't anticipate how the products will be implemented.

Solution

Structure

 

Behavior

Discussion

Assume abstract base classes are introduced for a family of related components/products:

class ComponentA {...};
class ComponentB {...};

An abstract factory/kit provides abstract factory methods for each of these components:

class Factory
{
public:
   virtual ComponentA* makeComponentA() = 0;
   virtual ComponentB* makeComponentB() = 0;
   // etc.
};

An assembly constructor can now be parameterized by a factory that makes the necessary components:

class Assembly
{
private:
   ComponentA* a;
   ComponentB* b;
   // etc.
public:
   Assembly(Factory* f)
   {
      a = f->makeComponentA();
      b = f->makeComponentB();
      // etc.
   }
};