๐Ÿ Design Patterns with Python
Home Chapters Examples Diagrams Bibliography GitHub โ†—
  1. Home
  2. Creating Objects Under Control
Part 6 3 chapters

Creating Objects Under Control

Factory Method and Abstract Factory, Builder with dataclasses and Prototype, and Singleton as a module, a Borg, or a metaclass.

Chapter 16

Factory Method and Abstract Factory

Factory Method dissolves into a class or a Self classmethod in a Callable slot, while Abstract Factory survives as one frozen kit per carrier family, with mypy tying only the products that pass values to each other.

Read โ†’
Chapter 17

Builder, dataclasses, and Prototype

Builder and Prototype dissolve into a frozen keyword-only dataclass that validates once and replace for copies, with the field types deciding what a shallow copy shares.

Read โ†’
Chapter 18

Singleton: module, Borg, and metaclass

Singleton needs disambiguating: the default is a module built at import time or on first use, with a parameter for tests and a double-checked lock for loads that need run-time input.

Read โ†’
โ† Previous part Selecting and Varying Behaviour Next part โ†’ Composing and Adapting Structure
Design Patterns with Python
Prose, diagrams and decks: CC BY 4.0. Sample code: Apache 2.0.
Source on GitHub โ†—  ยท  File an issue โ†—