liveBook · Manning

Abstract factory design pattern

As explained in Object-Oriented Software Design in C++

A creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes.

When an application creates several related objects, independently choosing each concrete object can mix incompatible families. The Abstract Factory Design Pattern supplies an interface for creating families of related or dependent objects without naming their concrete classes. section 9.2 section 9.3

The pattern separates family selection from object construction. An abstract factory declares creation operations; concrete factories implement those operations for one family. Clients use the appropriate factory instead of constructing family members directly. section 9.2.2

A typical structure contains:

  • An abstract factory such as ProvisionsFactory, declaring operations such as make_players() and make_venue(). section 9.2.2
  • Concrete factories such as VarsityFactory and IntramuralFactory, each producing objects from one consistent family. section 9.2.2
  • Client classes such as VarsityDept and IntramuralDept, which select a factory but do not know how it creates players or venues. section 9.2.2
  • Abstract product interfaces such as PlayerStrategy and VenueStrategy, with family-specific implementations behind them. section 9.2.3

Keeping athletic provisions consistent

VarsityFactory creates varsity player and venue objects, while IntramuralFactory creates intramural equivalents. VarsityDept passes a VarsityFactory to sports such as VarsityBaseball; IntramuralDept passes an IntramuralFactory. The resulting families remain together: varsity sports use varsity players and stadiums, while intramural sports use intramural players and open fields. section 9.2.2

Figure 9.4 This version of the application is modeled from the Abstract Factory Design Pattern. Interface ProvisionsFactory is the abstract factory. Implementor class VarsityFactory creates the varsity family of player and venue objects, and therefore, VarsityDept uses VarsityFactory. Implementor class IntramuralFactory creates the intramural family of player and venue objects, and therefore, IntramuralDept uses IntramuralFactory. The grayed-out portions of the diagram have not changed logically from figure 9.2.

Use the class diagram to trace the abstract factory, concrete factories, client departments, and related product families. section 9.2.2

How Object-Oriented Software Design in C++ covers it

How deep it goes
Explains the definition, roles, applied class structure, and concrete object flow, but does not detail implementation beyond the example.
Examples
Uses varsity and intramural sports, player strategies, venues, factories, department clients, class diagrams, and C++ listings.
What it assumes
Assumes familiarity with design patterns, inheritance, interfaces, and object creation while contrasting the pattern with Factory Method.
What you can do after
Apply it by defining a factory interface, implementing one factory per family, and passing the selected factory to clients or products.
How it connects
Connects abstract factories with factory methods, inheritance, client interfaces, abstract products, and the Strategy Design Pattern.

Also explained in

Compare how 3 books explain abstract factory design pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.