/design-patterns:creationalfrom design-patterns
Use when object creation is the source of pain — constructors growing too complex, the same setup repeated, unclear which concrete type to instantiate, or a shared instance causing hidden coupling. Apply before writing any factory, builder, or instantiation logic. Covers all 5 GoF creational patterns: Singleton, Builder, Factory Method, Abstract Factory, Prototype.
Ships in design-patterns. 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.
None: SKILL.md is the whole skill.
Source: Gang of Four — Design Patterns (1994)
| Question | Pattern |
|---|---|
| Truly need exactly one shared instance? | Singleton |
| Construction is complex or has many valid combinations? | Builder |
| Subclass or context decides the concrete type? | Factory Method |
| Need a matched family of related objects? | Abstract Factory |
| Cloning existing instance is cheaper than rebuilding? | Prototype |
Ensures a class has exactly one instance and provides a global access point to it.
Pain it removes: the need to coordinate access to a truly shared resource — a process-wide logger, a connection pool, a read-only configuration snapshot.
When to use: the object is effectively stateless or safe to share across the entire process. Examples: read-only config snapshot, process-wide logger wrapper, thread pool.
When NOT to use: when "easy access" is the only motivation. Singleton hides dependencies, makes tests interfere with each other, and is difficult to reset between test runs. If what you want is controlled wiring, dependency injection is almost always better.
class Config:
_instance: 'Config | None' = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._load()
return cls._instance
def _load(self):
self._values = load_from_environment()
def get(self, key: str) -> str:
return self._values[key]
Risky when: the singleton stores mutable state, request context, user sessions, or anything that must be reset between tests or requests.
Better alternative in most cases: pass the shared dependency explicitly via constructor injection, and let a DI container manage its lifecycle.
Separates the construction of a complex object from its representation, allowing the same process to produce different valid configurations.
Pain it removes: constructors with many optional parameters where invalid combinations are easy to pass and hard to detect at the call site.
When to use: objects with many optional parameters; valid combinations that must be validated before the object is created; immutable objects that require all fields before construction; "known good" presets you want to expose without requiring callers to specify everything.
# Without Builder — hard to read, easy to misuse
request = Request(url, method, headers, body, timeout, retry_count, cache_policy, auth)
# With Builder — intent is clear, validation happens at build()
request = (RequestBuilder()
.url("https://api.example.com/orders")
.method("POST")
.headers(auth_headers)
.timeout(seconds=2)
.build()) # validates required fields and invalid combinations here
class RequestBuilder:
def __init__(self):
self._url = None
self._method = "GET"
self._headers = {}
self._timeout = 30
def url(self, url: str) -> 'RequestBuilder':
self._url = url
return self
def method(self, method: str) -> 'RequestBuilder':
self._method = method
return self
def headers(self, headers: dict) -> 'RequestBuilder':
self._headers = headers
return self
def timeout(self, seconds: int) -> 'RequestBuilder':
self._timeout = seconds
return self
def build(self) -> Request:
if not self._url:
raise ValueError("url is required")
return Request(self._url, self._method, self._headers, self._timeout)
When NOT to use: when all parameters are required (a constructor is clearer); when the object has few fields (keyword arguments solve it); when the language already provides named/optional arguments that make intent clear.
Defines an interface for creating an object, but lets subclasses or implementations decide which class to instantiate. Defers instantiation to subclasses.
Pain it removes: creation logic that varies by subclass — the base class needs to create objects but should not be coupled to their concrete types.
When to use: a base class defines a workflow that includes creating objects, and subclasses must control what gets created; plugin architectures where the framework creates objects defined by the application.
class DocumentCreator:
def create_document(self) -> Document:
raise NotImplementedError
def open_and_render(self, path: str) -> None:
doc = self.create_document() # factory method
doc.load(path)
doc.render()
class WordDocumentCreator(DocumentCreator):
def create_document(self) -> Document:
return WordDocument()
class PdfDocumentCreator(DocumentCreator):
def create_document(self) -> Document:
return PdfDocument()
Simpler alternative — a standalone factory function — is often cleaner when you do not need inheritance:
def create_payment_processor(provider: str) -> PaymentProcessor:
registry = {
'stripe': StripeProcessor,
'paypal': PayPalProcessor,
'bank_transfer': BankTransferProcessor,
}
cls = registry.get(provider)
if cls is None:
raise ValueError(f"Unknown provider: {provider}")
return cls()
When NOT to use: when the type is always the same (instantiate directly); when a simple dictionary dispatch is sufficient.
Provides an interface for creating families of related objects without specifying their concrete classes. Ensures that objects from the same family are used together.
Pain it removes: needing to create sets of objects that must be compatible with each other (e.g., UI components from the same platform, infrastructure components for the same cloud provider).
When to use: you need to enforce that a set of related objects (button + dialog + input, or client + serialiser + validator) all come from the same "family"; you need to switch entire families at once.
class UIFactory:
def create_button(self) -> Button: ...
def create_dialog(self) -> Dialog: ...
def create_input(self) -> Input: ...
class WebUIFactory(UIFactory):
def create_button(self) -> Button: return HtmlButton()
def create_dialog(self) -> Dialog: return HtmlDialog()
def create_input(self) -> Input: return HtmlInput()
class NativeUIFactory(UIFactory):
def create_button(self) -> Button: return NativeButton()
def create_dialog(self) -> Dialog: return NativeDialog()
def create_input(self) -> Input: return NativeInput()
class App:
def __init__(self, ui: UIFactory):
self._button = ui.create_button()
self._dialog = ui.create_dialog()
When NOT to use: when products are unrelated and do not need to be consistent; when there is genuinely only one product family now and no realistic expectation of a second.
Warning: Abstract Factory is one of the most over-applied patterns. The indirection cost is real — only introduce it when multiple families and family-switching are actual requirements.
Creates new objects by cloning an existing, fully configured instance rather than constructing from scratch.
Pain it removes: expensive or complex initialisation that would need to be repeated every time a new object of the same configuration is needed.
When to use: initialisation is expensive (database lookup, network call, complex computation) and new objects are variations of a known template; game objects sharing expensive setup but with unique position/state.
import copy
class ReportTemplate:
def __init__(self):
self._layout = self._load_layout() # expensive
self._styles = self._load_styles() # expensive
self._data = {}
def clone(self) -> 'ReportTemplate':
return copy.deepcopy(self) # cheap copy of expensive setup
def with_data(self, data: dict) -> 'ReportTemplate':
clone = self.clone()
clone._data = data
return clone
# Build once, clone many
base_template = ReportTemplate()
monthly_report = base_template.with_data(monthly_data)
quarterly_report = base_template.with_data(quarterly_data)
When NOT to use: when construction is cheap and cloning would be more complex than constructing; when deep copy semantics are unclear (shared mutable references in the clone).
build() validates all required fields and invalid combinations before constructingself to enable fluent chainingWhat SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub entelligentsia/skillforge --plugin design-patterns10 skills in this plugin
/anti-corruptionUse when external system types, vocabulary, or conventions bleed into your domain model, a single provider change would require touching many internal files, or a legacy system must keep running while a new one is built alongside it. Apply before writing any integration with an external API, legacy codebase, or third-party service. Covers Anti-Corruption Layer, Gateway, Facade, and Strangler Fig./behaviouralUse when behaviour varies across cases, accumulates conditionals, or needs to be decoupled, queued, undone, or observed. Apply when a method is growing if/switch branches, an algorithm differs per context, a pipeline needs flexible steps, or objects need to react to each other without tight coupling. Covers all 10 GoF behavioural patterns./cqrsUse when read and write performance requirements diverge significantly, complex reporting is painful through the domain model, or you need to scale reads and writes independently. Apply before splitting any command/query handler or building read-optimised projections. Covers Command Query Responsibility Segregation — including when it is overkill — with guidance on applying only as much separation as the problem demands./data-accessUse when persistence logic bleeds into domain or business code, tests require a real database to verify business logic, ORM types appear in service or handler code, or switching storage would require touching domain classes. Apply before writing any database query, repository, or ORM integration. Covers Repository, Unit of Work, Data Mapper, and Active Record with explicit guidance on when each applies./domain-eventsUse when aggregates need to stay loosely coupled, side effects (emails, audit logs, projections) are tangled into domain operations, or one aggregate directly calls methods on another. Apply when something significant happens in the domain that other parts of the system need to react to without being tightly coupled. Covers Domain Events, publishing patterns, synchronous vs asynchronous handling, idempotency, and eventual consistency trade-offs./domain-modelingUse when business objects have no behaviour (anemic model), invariants are enforced in services or controllers instead of domain classes, ownership of business rules is unclear, or the code allows states the business forbids. Apply before designing or reviewing any business domain class. Covers DDD tactical patterns: Entity, Value Object, Aggregate, Aggregate Root.Other plugins' skills for the same job