/design-patterns-gof:creational-pattern-selectorfrom design-patterns-gof
Helps select the right creational design pattern (Abstract Factory, Builder, Factory Method, Prototype, Singleton) for object creation problems. Compares patterns by subclassing vs composition trade-offs.
Ships in design-patterns-gof. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
You have an object creation problem that needs a structural solution. This skill applies when:
new ConcreteClass() calls in places where the class type should be variableBefore starting, verify you have a creation problem — not a structural or behavioral one. If you're not sure whether it's a creation problem at all, invoke the design-pattern-selector skill first, which will classify the problem and route here if appropriate.
new ConcreteClass() calls scattered across multiple files — indicates Factory Method or Abstract Factoryclone() or copy-constructor patterns — indicates Prototype already in use or neededACTION: Use TodoWrite to track steps, then articulate what aspect of creation is inflexible.
WHY: Creational patterns solve five distinct problems. The right pattern becomes obvious once you identify which of the five matches your situation. Without this framing, patterns get conflated (e.g., using Builder when Factory Method would suffice).
TodoWrite:
- [ ] Step 1: Frame the creation problem
- [ ] Step 2: Identify the parameterization strategy
- [ ] Step 3: Check the five applicability checklists
- [ ] Step 4: Consider the evolution path
- [ ] Step 5: Produce scored recommendation with trade-offs
Capture these elements:
| Element | Question |
|---|---|
| Product type | Single object, family of related objects, or complex composite object? |
| Variation axis | What needs to vary — the concrete class, the construction steps, or the state? |
| Flexibility timing | Must creation be flexible at compile-time or at run-time? |
| Uniqueness constraint | Must the system allow only one instance? |
ACTION: Determine which of the two fundamental strategies applies to this problem.
WHY: The GoF Discussion of Creational Patterns identifies two recurring ways to make a system independent of how objects are created. Understanding which strategy you're using eliminates half the candidate patterns immediately and reveals the underlying design commitment you're making.
Strategy 1 — Subclass-based (class parameterization): The class that creates objects is subclassed to change what gets created. The creator class calls a virtual factory method; subclasses override it to return different product types.
Strategy 2 — Composition-based (object parameterization): An external object is responsible for knowing which concrete classes to create. That object is passed into the creator as a parameter or configured at startup.
Output of this step: Identify which strategy fits. If the flexibility is needed at run-time or affects multiple product types, lean toward composition-based (Strategy 2). If a single virtual method suffices, consider Factory Method (Strategy 1).
ACTION: Run the problem against each pattern's applicability criteria. Mark which conditions are satisfied.
WHY: Each creational pattern has a precise set of conditions that distinguish it from the others. Checking all five prevents the common mistake of picking the most familiar pattern rather than the most fitting one. A pattern with 3 of 3 matching conditions beats one with 2 of 4.
Detailed applicability criteria are in references/creational-comparison.md. Summary checklists:
Abstract Factory — use when ALL of these hold:
Example trigger: UI toolkit supporting multiple look-and-feel standards — Motif or Presentation Manager. Each look-and-feel is a product family (windows, scrollbars, buttons). Swapping the factory swaps the entire family consistently.
Builder — use when ALL of these hold:
Example trigger: A document converter that reads one format (RTF) and can produce multiple output formats (ASCII text, TeX, interactive widget). The parsing algorithm (Director) is stable; only the converter object (Builder) changes.
Factory Method — use when ANY of these hold:
Example trigger: An application framework that manages Document objects, but the framework cannot know which Document subclass the application will use. The framework calls a virtual CreateDocument() factory method; application subclasses override it.
Prototype — use when ALL of these hold:
Example trigger: A graphical editor where each tool creates graphic objects. Instead of subclassing the tool for each graphic type (subclass explosion), each tool is parameterized with a prototype it clones when the user draws.
Singleton — use when BOTH of these hold:
Example trigger: A maze game where all game objects need access to the Maze — without passing the maze as a parameter everywhere. Singleton ensures one maze exists and provides a well-known access point.
ACTION: Assess where the system is now and where it needs to go. Map that against the natural evolution path.
WHY: The GoF explicitly states that designs often start with Factory Method and evolve toward Abstract Factory, Builder, or Prototype as flexibility needs grow. Starting with the most flexible pattern when Factory Method would suffice is over-engineering. But starting with Factory Method when Abstract Factory is needed leads to a painful refactor. Knowing the trajectory saves both mistakes.
Evolution path:
Factory Method
|
| → When you need multiple product families → Abstract Factory
|
| → When construction is complex/multi-step → Builder
|
| → When subclass proliferation is the pain → Prototype
|
| → When you need one shared instance → Singleton (orthogonal — often combined with the above)
Trajectory questions:
Maze game comparison (from the GoF):
The MazeGame::CreateMaze() function is the baseline: it hard-codes new Room, new Wall, new Door. To support enchanted mazes (EnchantedRoom, DoorNeedingSpell), each pattern applies differently:
| Pattern | How CreateMaze changes |
|---|---|
| Factory Method | MazeGame calls virtual MakeRoom(), MakeWall(), MakeDoor(). EnchantedMazeGame overrides them. |
| Abstract Factory | CreateMaze is passed a MazeFactory parameter. EnchantedMazeFactory returns enchanted parts. |
| Builder | CreateMaze is passed a MazeBuilder object. Builder constructs the maze incrementally. |
| Prototype | CreateMaze is passed prototypical Room, Wall, Door instances it clones. |
| Singleton | MazeFactory is a Singleton — one factory per application, globally accessible. |
ACTION: Deliver a structured recommendation in the format below, based on the steps above.
WHY: A clear recommendation with explicit trade-offs enables the developer to commit confidently or challenge the reasoning. Surfacing the runner-up pattern and why it was ruled out prevents second-guessing after implementation starts.
Recommendation format:
## Creational Pattern Recommendation: [Pattern Name]
**Parameterization strategy:** [Subclass-based / Composition-based]
**Why this pattern:** [1–2 sentences: connect the pattern's applicability conditions to the specific problem]
**What it decouples:** [What concrete class references are eliminated from the caller]
**What you gain:** [Key benefit — flexibility, consistency, construction control, etc.]
**Key trade-off:** [Cost — added classes, complexity, or constraint]
**Evolution note:** [When to migrate to a more flexible pattern, if applicable]
**Alternative considered:** [Runner-up] — [Why it was not chosen]
**Compatible with:** [Other patterns that combine well, e.g., Singleton + Abstract Factory]
Scenario: A UI toolkit must support two look-and-feel standards (Motif and a custom dark theme). Both have windows, buttons, and scrollbars. Clients should be able to switch the entire look-and-feel without knowing which concrete widget classes they're using.
Trigger: "We need to support multiple themes and the theme selection should be swappable at startup without touching client code."
Process:
Output:
## Creational Pattern Recommendation: Abstract Factory
**Parameterization strategy:** Composition-based
**Why this pattern:** There are multiple coordinated product families (widget sets per look-and-feel),
and clients must use widgets from one consistent family. Abstract Factory encapsulates this
family constraint as a factory object passed to clients.
**What it decouples:** All concrete widget class names from client code. Clients
only reference abstract Widget interfaces and the WidgetFactory interface.
**What you gain:** Swapping the entire look-and-feel is a single factory substitution.
The constraint that Motif widgets go with Motif scrollbars is enforced automatically.
**Key trade-off:** Adding a new widget type (e.g., a tooltip) requires changing the
AbstractFactory interface and all ConcreteFactory implementations.
**Evolution note:** If themes are runtime-configurable (loaded from disk, user preference),
combine with Prototype — register prototypical widget instances per theme.
**Alternative considered:** Factory Method — would require subclassing the client application
for each theme, which does not scale when themes are selected at run-time.
Scenario: An application reads one input format (Markdown) and must produce different output representations: plain text, HTML, and a rich text widget for in-app editing. The parsing algorithm is stable; only the output format changes.
Trigger: "The same parsing logic needs to produce completely different output formats. Adding a new format shouldn't require touching the parser."
Process:
Output:
## Creational Pattern Recommendation: Builder
**Parameterization strategy:** Composition-based
**Why this pattern:** The parsing algorithm (Director) is stable and orthogonal to
how the output format is assembled. Builder separates the algorithm from the
representation by providing the parser with a converter interface (Builder) that
each output format implements independently.
**What it decouples:** The parser from all output format classes. The parser calls
BuildParagraph(), BuildHeading() — it never references HTMLConverter or TextConverter.
**What you gain:** New output format = new ConcreteBuilder class. Zero changes to parser.
Formats can have internal state (e.g., tracking open tags in HTML) hidden from the parser.
**Key trade-off:** Builder requires a Director–Builder collaboration; it adds more
structure than directly parameterizing with a conversion function. Justified when
construction is genuinely multi-step with internal state.
**Evolution note:** If the parser needs to support multiple input formats too,
Abstract Factory could manage the pairing of reader + builder per format combination.
**Alternative considered:** Abstract Factory — suited for families of related objects,
not for a single complex object assembled step-by-step from sequential operations.
Scenario: A maze game's CreateMaze() method directly instantiates Room, Wall, and Door with new. The team wants to introduce an enchanted maze variant with EnchantedRoom and DoorNeedingSpell without duplicating the construction logic.
Trigger: "We want to reuse the maze creation algorithm for different maze variants, but it's hard-coded to specific classes."
Process:
MakeRoom(), MakeWall(), MakeDoor() to MazeGame and overriding them in EnchantedMazeGame. Evolve to Abstract Factory only if maze variants proliferate and need to be swappable at runtime.Output:
## Creational Pattern Recommendation: Factory Method (evolve to Abstract Factory if needed)
**Parameterization strategy:** Subclass-based (Factory Method) — composition-based
(Abstract Factory) when variants must be runtime-swappable
**Why this pattern:** MazeGame knows when to create rooms and doors but should not
decide which concrete type. Factory Method adds virtual creation methods; EnchantedMazeGame
overrides them. The construction algorithm in CreateMaze() is unchanged.
**What it decouples:** MazeGame from the concrete component classes Room, Wall, Door.
CreateMaze() calls MakeRoom() instead of new Room().
**What you gain:** Adding a new maze variant is one new subclass of MazeGame with
three method overrides. No duplication of the construction algorithm.
**Key trade-off:** Every new maze variant requires a new MazeGame subclass. If variants
must be selected at runtime (not compile-time), this becomes awkward — that signals
the need to migrate to Abstract Factory.
**Evolution note:** When maze types need to be chosen at startup or loaded from config,
replace the subclass approach with an AbstractMazeFactory passed to CreateMaze(). The
factory object becomes a Singleton if only one maze type is active per application run.
**Alternative considered:** Abstract Factory — more flexible but requires an extra
factory object hierarchy from the start; over-engineered if compile-time subclassing is sufficient.
| File | Contents |
|---|---|
references/creational-comparison.md | Full applicability, consequences, and trade-off table for all 5 creational patterns; maze game comparison; GraphicTool example |
This skill is licensed under CC-BY-SA-4.0. Source: BookForge — Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides.
Install related skills from ClawhHub:
clawhub install bookforge-design-pattern-selectorOr install the full book set from GitHub: bookforge-skills
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub bookforge-ai/bookforge-skills --plugin design-patterns-gof15 skills in this plugin
/abstract-factory-implementorImplement the Abstract Factory pattern to create families of related objects without specifying their concrete classes. Use when a system must be independent of how its products are created, when it must work with multiple product families, when products are designed to be used together and you need to enforce that constraint, or when providing a class library revealing only interfaces. Covers factory-as-singleton, prototype-based factories, and extensible factory interfaces.
/behavioral-pattern-selectorChoose the right behavioral design pattern from the 11 GoF behavioral patterns. Use when someone asks "how do I make this algorithm swappable?", "should I use Strategy or State?", "how do I decouple senders from receivers?", "what's the difference between Observer and Mediator?", "how do I add undo/redo?", "how do I traverse a collection without exposing its internals?", or "how do I add operations to classes I can't modify?" Also use when you see switch statements selecting between algorithms, tightly coupled event handlers, objects that change behavior based on state, or request-handling logic that should be distributed across a chain. Analyzes via two taxonomies — what aspect to encapsulate as an object (algorithm, state, protocol, traversal) and how to decouple senders from receivers (Command, Observer, Mediator, Chain of Responsibility) — with explicit trade-offs for each choice.
Other plugins' skills for the same job