From grimoire
Provides instructions for implementing the Prototype design pattern (cloning objects) in Python, JavaScript, and Java. Useful when object creation is expensive or runtime type is unknown.
How this skill is triggered — by the user, by Claude, or both
Slash command
/grimoire:apply-prototype-patternThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Create new objects by copying an existing prototype rather than constructing from scratch.
Create new objects by copying an existing prototype rather than constructing from scratch.
Adopted by: JavaScript (the prototype-based object model is the language's core
inheritance mechanism; Object.create() is a direct implementation), Python (copy
module's copy() and deepcopy() are standard library prototype operations), Java
(Cloneable interface and Object.clone()), and Unity game engine (prefab
instantiation is a prototype clone operation at the engine level).
Impact: GoF documents the prototype's primary benefit: when the cost of creating an
object is significantly greater than the cost of copying one, prototypes reduce
construction cost to memcopy speed. In game engines (Unity), instantiating thousands of
enemies from a single prefab prototype is an order-of-magnitude faster than constructing
each from scratch with physics body initialization.
Why best: The alternative — reconstructing the full object state every time — is
wasteful when the state was expensive to compute. The prototype also eliminates
coupling to constructor signatures: the client copies the object without knowing its
concrete class.
Sources: Gamma et al. (1994) pp. 117–126; Python copy module documentation;
Unity Manual: Instantiating Prefabs
clone() method to the prototype interfacefrom abc import ABC, abstractmethod
import copy
class Prototype(ABC):
@abstractmethod
def clone(self) -> "Prototype": ...
clone() using shallow or deep copyclass GameEnemy(Prototype):
def __init__(self, name: str, health: int, position: tuple, inventory: list):
self.name = name
self.health = health
self.position = position
self.inventory = inventory
def clone(self) -> "GameEnemy":
return copy.deepcopy(self) # deep copy: inventory list is independent
Use deepcopy when the prototype contains mutable nested objects that each clone
should own independently. Use copy (shallow) only when nested objects are shared
by design or are immutable.
class EnemyRegistry:
def __init__(self):
self._prototypes: dict[str, GameEnemy] = {}
def register(self, name: str, prototype: GameEnemy) -> None:
self._prototypes[name] = prototype
def create(self, name: str) -> GameEnemy:
prototype = self._prototypes.get(name)
if prototype is None:
raise KeyError(f"no prototype registered for '{name}'")
return prototype.clone()
registry = EnemyRegistry()
registry.register("orc", GameEnemy("Orc", 100, (0, 0), ["sword"]))
# Later — create many orcs cheaply
orc1 = registry.create("orc")
orc2 = registry.create("orc")
After cloning, modify only the clone's instance-specific state:
orc1.position = (10, 5)
orc1.health = 80 # damaged variant
# registry's prototype is unchanged
new Object() is nearly free, cloning adds complexity with no benefit.Shallow copy when deep copy is needed. If the prototype holds a list, a shallow copy shares the list between prototype and clone. Mutating the clone's list mutates the prototype. Use deepcopy unless sharing is intentional.
Modifying the prototype instead of the clone. The registry prototype is the template. Any mutation to it affects all future clones. Treat registered prototypes as read-only after registration.
Implementing clone() by calling the constructor. Calling ConcreteProduct(self.field1, self.field2, ...) in clone() defeats the pattern — it re-couples clone() to the constructor signature. Use copy.deepcopy(self) or equivalent.
npx claudepluginhub jeffreytse/grimoire --plugin grimoireClones objects using prototype registry and structuredClone for deep copy scenarios. Useful when constructing objects is expensive (network calls, heavy computation) and cloning is cheaper.
Generates Prototype pattern implementations for PHP 8.4: deep/shallow clones, clone customization, prototype registries, immutable object duplication.
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.