Abstract Factory

Provides an interface for creating families of related or dependent objects without specifying their concrete classes. Use it when a system should be independent of how its products are created, composed, and represented, or when it must be configured with one of multiple families of products that are designed to be used together.

Examples:

  • A furniture shop simulator needs chairs, sofas, and coffee tables in three styles: Modern, Victorian, and Art Deco. An AbstractFurnitureFactory interface declares createChair(), createSofa(), and createCoffeeTable(); concrete factories (ModernFurnitureFactory, VictorianFurnitureFactory) implement it, ensuring all items a client receives belong to the same style family.
// AbstractFactory declares creation interface
class WidgetFactory {
public:
  virtual ScrollBar* CreateScrollBar() = 0;
  virtual Button*    CreateButton()    = 0;
};
// ConcreteFactory implements one product family
class MotifWidgetFactory : public WidgetFactory {
public:
  ScrollBar* CreateScrollBar() override { return new MotifScrollBar; }
  Button*    CreateButton()    override { return new MotifButton; }
};

Synonyms: kit