JS Factory Pattern
Create objects via a factory function without exposing instantiation logic to callers
When to Use
- Object creation logic is complex and should be encapsulated
- The exact type of object to create is determined at runtime
- You want to return different implementations based on input without exposing
newto callers
Instructions
- Write a factory function (or static class method) that accepts configuration and returns the correct object.
- The caller never uses
newdirectly — they call the factory. - Validate inputs inside the factory before creating the object.
- Return a consistent interface regardless of which concrete type was created.
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']
Details
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:
- Callers cannot use
instanceofto check the type (unless the factory returns a class instance) - Can become a large switch/if-else block if not maintained
When NOT to use:
- When all instances are always the same type — just use
newdirectly - For very simple objects with no creation logic — plain object literals are sufficient
Source
https://patterns.dev/javascript/factory-pattern
Process
- Read the instructions and examples in this document.
- Apply the patterns to your implementation, adapting to your specific context.
- Verify your implementation against the details and edge cases listed above.
Harness Integration
- Type: knowledge — this skill is a reference document, not a procedural workflow.
- No tools or state — consumed as context by other skills and agents.
Success Criteria
- The patterns described in this document are applied correctly in the implementation.
- Edge cases and anti-patterns listed in this document are avoided.