From harness-claude
Clones objects using prototype registry and structuredClone for deep copy scenarios. Useful when constructing objects is expensive (network calls, heavy computation) and cloning is cheaper.
How this skill is triggered — by the user, by Claude, or both
Slash command
/harness-claude:gof-prototype-patternThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
> Clone objects using prototype registry and structured clone for deep copy scenarios.
Clone objects using prototype registry and structured clone for deep copy scenarios.
Explicit clone method (most control):
interface Cloneable<T> {
clone(): T;
}
class DocumentTemplate implements Cloneable<DocumentTemplate> {
constructor(
public title: string,
public sections: string[],
public metadata: Record<string, string>
) {}
clone(): DocumentTemplate {
return new DocumentTemplate(
this.title,
[...this.sections], // shallow copy of array
{ ...this.metadata } // shallow copy of object
);
}
}
// Use a cloned base instead of constructing from scratch
const invoiceTemplate = new DocumentTemplate(
'Invoice',
['Header', 'Line Items', 'Total', 'Footer'],
{ type: 'invoice', version: '1.0' }
);
const march = invoiceTemplate.clone();
march.title = 'Invoice - March 2024';
march.metadata['period'] = '2024-03';
structuredClone for deep copies (Node 17+, no custom classes):
interface ConfigSnapshot {
database: { host: string; port: number; pool: { min: number; max: number } };
features: string[];
limits: Record<string, number>;
}
function cloneConfig(config: ConfigSnapshot): ConfigSnapshot {
return structuredClone(config); // deep clone, no circular reference issues
}
const baseConfig: ConfigSnapshot = {
database: { host: 'localhost', port: 5432, pool: { min: 2, max: 10 } },
features: ['auth', 'billing'],
limits: { rateLimit: 100, timeout: 5000 },
};
const devConfig = cloneConfig(baseConfig);
devConfig.database.pool.max = 2; // doesn't affect baseConfig
devConfig.features.push('debug');
Prototype registry:
class ShapeRegistry {
private prototypes = new Map<string, Shape>();
register(name: string, prototype: Shape): void {
this.prototypes.set(name, prototype);
}
create(name: string): Shape {
const proto = this.prototypes.get(name);
if (!proto) throw new Error(`Unknown shape prototype: ${name}`);
return proto.clone();
}
}
// Pre-warm registry at startup
const registry = new ShapeRegistry();
registry.register('red-circle', new Circle({ radius: 10, color: 'red' }));
registry.register('blue-rect', new Rectangle({ width: 20, height: 10, color: 'blue' }));
// Clone cheaply at runtime
const shape1 = registry.create('red-circle');
const shape2 = registry.create('red-circle');
Object.assign for simple shallow clones:
class UserProfile {
constructor(
public name: string,
public email: string,
public preferences: { theme: string; locale: string }
) {}
withPreferences(overrides: Partial<typeof this.preferences>): UserProfile {
return Object.assign(new UserProfile(this.name, this.email, { ...this.preferences }), {
preferences: { ...this.preferences, ...overrides },
});
}
}
Deep vs. shallow clone — pick the right tool:
| Scenario | Tool |
|---|---|
| Simple POJOs, no class methods needed | structuredClone() |
| Class instances with methods | Explicit clone() method |
| Shallow one-level copy | { ...obj } or Object.assign({}, obj) |
| Arrays | [...arr] or arr.slice() |
structuredClone limitations: Cannot clone functions, class instances (loses prototype chain), DOM nodes, or WeakMap. If you need class instances, use an explicit clone() method.
Anti-patterns:
structuredClone handles this, JSON.parse(JSON.stringify()) does notJSON round-trip clone (legacy, avoid):
// Works for simple data, but loses: undefined, Date, functions, class prototypes
const copy = JSON.parse(JSON.stringify(original));
// Prefer structuredClone() instead
refactoring.guru/design-patterns/prototype
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeProvides 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.
Generates Prototype pattern implementations for PHP 8.4: deep/shallow clones, clone customization, prototype registries, immutable object duplication.
Explains the JavaScript prototype pattern for sharing methods across instances via the prototype chain. Includes trade-offs, ES6 class syntax, and antipatterns.