Factory Pattern
Factory Pattern
Section titled “Factory Pattern”Introduction
Section titled “Introduction”The Factory pattern provides an interface for creating objects without exposing the creation logic to the caller.
Topics Covered
Section titled “Topics Covered”- Implementation — Centralized object creation
- Real-World Examples — Practical factory scenarios
Implementation
Section titled “Implementation”class User { constructor(name) { this.name = name; this.role = 'user'; } }class Admin { constructor(name) { this.name = name; this.role = 'admin'; } }class Guest { constructor(name) { this.name = name; this.role = 'guest'; } }
function userFactory(type, name) { switch (type) { case 'admin': return new Admin(name); case 'guest': return new Guest(name); default: return new User(name); }}
const alice = userFactory('admin', 'Alice'); // Admin { name: 'Alice', role: 'admin' }const bob = userFactory('user', 'Bob'); // User { name: 'Bob', role: 'user' }Summary
Section titled “Summary”- Centralizes object creation
- Hides complex instantiation logic
- Easy to extend with new types