liveBook · Manning

Abstract factory design pattern

As explained in Software Design for Python Programmers

The Abstract Factory Design Pattern is a creational design pattern that provides an interface for creating families of related objects without specifying their concrete classes.

The Abstract Factory Design Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. It keeps objects from different families from being mixed accidentally. section 9.0 section 9.4

Use the pattern when one domain contains coordinated product families. In the athletics example, varsity and intramural sports require different combinations of player and venue objects. section 9.2 section 9.3

A practical implementation separates family selection from product construction:

  • Implement VarsityFactory and IntramuralFactory so each creates only products from its own family. section 9.2.2
  • Let VarsityDept and IntramuralDept select their corresponding factories while remaining unaware of construction details. section 9.2.2

Athletics departments and sport families

VarsityFactory can create VarsityBaseballPlayers or VarsityFootballPlayers and a Stadium; IntramuralFactory can create intramural player strategies and an OpenField. VarsityBaseball and IntramuralFootball then retain their family category while Sport obtains its collaborators through the selected factory.

Figure 9.4 This version of the application is modeled from the Abstract Factory Design Pattern. Interface ProvisionsFactory is the abstract factory whose implementor classes are used by the Sport subclasses. 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.
A screenshot of a computer

AI-generated content may be incorrect.

Use the architecture diagram to trace ProvisionsFactory, its concrete factories, department clients, and the player and venue product families. section 9.2.2

Unlike Factory Method, which creates one object with subclass-selected variants, Abstract Factory creates a coordinated family and reduces accidental cross-family combinations. section 9.3

How Software Design for Python Programmers covers it

How deep it goes
Explains the definition, roles, Python implementation, concrete products, architecture diagram, and contrast with Factory Method.
Examples
Uses varsity and intramural athletics, player strategies, venues, sports, factories, departments, diagrams, and Python listings.
What you can do after
Apply it via Python by defining an abstract factory, implementing family-specific factories, and injecting the selected factory into clients.
How it connects
Connects object families with Factory Method and algorithm families with 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 Software Design for Python Programmers. The section links point to the parts of the book it draws on.