From grimoire
Centralizes object creation for families of related objects, enabling easy swapping of product families without client code changes. Based on the GoF pattern.
How this skill is triggered — by the user, by Claude, or both
Slash command
/grimoire:apply-abstract-factory-patternThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Provide an interface for creating families of related objects without specifying their concrete classes.
Provide an interface for creating families of related objects without specifying their concrete classes.
Adopted by: Java AWT/Swing (the LookAndFeel system is an abstract factory for UI
components), .NET WPF theme factories, Apple AppKit (NSWindow/NSView factory hierarchy),
and Google Guice/Spring (module-level provider factories) — among the most downloaded
frameworks in each ecosystem.
Impact: GoF documents that Abstract Factory eliminates conditional logic that otherwise
appears at every object-creation site when switching product families. In practice, adding
a new UI theme in Java Swing requires implementing one factory class — not editing every
component constructor. The pattern makes "swap the whole family" a one-class change.
Why best: The alternative — instantiating concrete classes directly — scatters
product-family selection across the codebase. Every new WindowsButton() is a site that
must be updated when switching to MacButton. Abstract Factory centralizes that decision
in one place and enforces consistency: you cannot accidentally mix products from different
families (a Windows button with a Mac checkbox).
Sources: Gamma et al. (1994) pp. 87–95; Freeman & Freeman, "Head First Design Patterns" (2004) ch. 4; Java AWT documentation
Declare a creation method for each product type in the family:
from abc import ABC, abstractmethod
class UIFactory(ABC):
@abstractmethod
def create_button(self) -> Button: ...
@abstractmethod
def create_checkbox(self) -> Checkbox: ...
class Button(ABC):
@abstractmethod
def render(self) -> str: ...
class Checkbox(ABC):
@abstractmethod
def render(self) -> str: ...
class WindowsFactory(UIFactory):
def create_button(self) -> Button:
return WindowsButton()
def create_checkbox(self) -> Checkbox:
return WindowsCheckbox()
class MacFactory(UIFactory):
def create_button(self) -> Button:
return MacButton()
def create_checkbox(self) -> Checkbox:
return MacCheckbox()
class Application:
def __init__(self, factory: UIFactory):
self.button = factory.create_button()
self.checkbox = factory.create_checkbox()
def render(self):
print(self.button.render())
print(self.checkbox.render())
# Swap families without touching Application
app = Application(WindowsFactory())
app = Application(MacFactory())
Decide which family to use once — at startup, from config, or from environment — then
pass the factory down. Components never call WindowsFactory() directly.
apply-factory-method-pattern is simpler.Adding new product types to an existing factory. Adding create_tooltip() to UIFactory requires changing every concrete factory. This violates OCP. Prefer extending via composition rather than widening the factory interface.
Using Abstract Factory for a single product. If you only have one kind of product, Factory Method is the right pattern — Abstract Factory is for families.
Putting factory selection logic inside a component. if os == "windows": factory = WindowsFactory() inside a render() method defeats the point. Selection belongs at the composition root.
npx claudepluginhub jeffreytse/grimoire --plugin grimoireImplements the GOF Abstract Factory pattern to create families of related objects (e.g., UI themes, cloud SDKs) without coupling to concrete types.
Applies the Factory Method pattern to delegate object creation to subclasses or configuration, reducing coupling and enabling extensibility.
Decision guide for the 23 Gang of Four patterns. Maps code symptoms and goals to the right pattern, then loads the structural recipe.