From harness-claude
Creates objects via factory functions without exposing instantiation logic to callers. Encapsulates complex creation, runtime type selection, and input validation.
How this skill is triggered — by the user, by Claude, or both
Slash command
/harness-claude:js-factory-patternThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
> Create objects via a factory function without exposing instantiation logic to callers
Create objects via a factory function without exposing instantiation logic to callers
new to callersnew directly — they call the factory.function createUser(type, name) {
const roles = {
admin: { permissions: ['read', 'write', 'delete'] },
editor: { permissions: ['read', 'write'] },
viewer: { permissions: ['read'] },
};
if (!roles[type]) throw new Error(`Unknown user type: ${type}`);
return {
name,
type,
...roles[type],
greet() {
return `Hi, I'm ${name} (${type})`;
},
};
}
const admin = createUser('admin', 'Alice');
console.log(admin.permissions); // ['read', 'write', 'delete']
The Factory pattern is one of the most common patterns in JavaScript. Unlike new ClassName(), a factory function can return different types, apply caching, run validation, or perform async initialization.
Trade-offs:
instanceof to check the type (unless the factory returns a class instance)When NOT to use:
new directlyhttps://patterns.dev/javascript/factory-pattern
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeImplements the Abstract Factory creational pattern in JavaScript to create families of related objects (e.g., UI themes) without specifying concrete classes. Useful when product families must be swapped at runtime.
Applies the Factory Method pattern to delegate object creation to subclasses or configuration, reducing coupling and enabling extensibility.
Generates DDD-compliant factories for PHP 8.4 domain objects, encapsulating complex creation logic with input validation and unit tests. Ideal for intricate entity instantiation, aggregates, and reconstruction from persistence.