From grimoire
Guides applying the Builder pattern to simplify construction of complex objects with many parameters, reducing telescoping constructors.
How this skill is triggered — by the user, by Claude, or both
Slash command
/grimoire:apply-builder-patternThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Separate the construction of a complex object from its representation so the same process can create different representations.
Separate the construction of a complex object from its representation so the same process can create different representations.
Adopted by: Java's StringBuilder (the most-used Java class by call frequency),
Lombok @Builder (used in >50% of large Java codebases per JetBrains developer survey
2023), Kotlin's buildString/buildList, Google Protocol Buffers (.newBuilder()),
and Go's functional options pattern — a Builder variant adopted as idiomatic Go.
Impact: Telescoping constructors (10-parameter constructors) are cited in Clean Code
(Martin, 2008) as one of the top readability defects in Java codebases. The Builder
pattern eliminates this directly: Apache HttpClient's request builder reduced a
12-parameter constructor to a fluent 4-step build chain, eliminating a documented class
of parameter-order bugs in their public API.
Why best: The alternative — telescoping constructors or a large configuration map —
either forces callers to pass nulls/defaults for unused fields (error-prone), or loses
type safety (map-based config). Builder provides named, typed, optional parameters
without changing the target object's constructor signature.
Sources: Gamma et al. (1994) pp. 97–106; Martin, "Clean Code" (2008) ch. 3; Lombok @Builder documentation; Google Protocol Buffers design docs
class Pizza:
def __init__(self, size: str, cheese: bool, pepperoni: bool, mushrooms: bool):
self.size = size
self.cheese = cheese
self.pepperoni = pepperoni
self.mushrooms = mushrooms
def __repr__(self):
toppings = [t for t, v in [("cheese", self.cheese),
("pepperoni", self.pepperoni),
("mushrooms", self.mushrooms)] if v]
return f"Pizza({self.size}, {', '.join(toppings) or 'plain'})"
class PizzaBuilder:
def __init__(self, size: str):
self._size = size
self._cheese = False
self._pepperoni = False
self._mushrooms = False
def with_cheese(self) -> "PizzaBuilder":
self._cheese = True
return self
def with_pepperoni(self) -> "PizzaBuilder":
self._pepperoni = True
return self
def with_mushrooms(self) -> "PizzaBuilder":
self._mushrooms = True
return self
def build(self) -> Pizza:
return Pizza(self._size, self._cheese, self._pepperoni, self._mushrooms)
# Before — telescoping constructor, unclear which True is which
pizza = Pizza("large", True, False, True)
# After — self-documenting, order-independent
pizza = (PizzaBuilder("large")
.with_cheese()
.with_mushrooms()
.build())
Put validation — required fields, mutually exclusive options — in build(), not in
each setter. This lets callers construct the builder in any order before committing.
def build(self) -> Pizza:
if not self._size:
raise ValueError("size is required")
return Pizza(self._size, self._cheese, self._pepperoni, self._mushrooms)
class ThinCrustBuilder(PizzaBuilder): ...
class DeepDishBuilder(PizzaBuilder): ...
director = PizzaDirector()
director.build_margherita(ThinCrustBuilder("medium"))
director.build_margherita(DeepDishBuilder("large"))
dataclass. Builder adds overhead for no benefit.Forgetting to call build() — callers get a Builder instead of the product. In typed languages, the return type prevents this. Document it in the builder's docstring.
Mutable builders shared across threads — a Builder holds mutable intermediate state. Don't share Builder instances between threads without synchronization.
Validating in setters instead of build() — validating in setters forces call order and prevents deferred construction. Validate at build time only.
npx claudepluginhub jeffreytse/grimoire --plugin grimoire2plugins reuse this skill
First indexed Jun 9, 2026
Implements the Builder pattern with fluent API and director classes. Useful for constructing complex objects with many optional parameters, or building different representations from the same construction process.
Generates Builder pattern for PHP 8.4 classes with fluent interface, validation, optional director, and unit tests. For complex objects with many parameters or optional fields.
Ensures a class has exactly one instance with a global access point. Useful for configuration stores, connection pools, and loggers where multiple instances would cause resource conflicts.