---
name: factory-method
description: This skill should be used when the user asks about "factory method pattern", "factory pattern", "when to use factory", "implement factory method", "creator pattern", "virtual constructor", or mentions needing to delegate object creation to subclasses.
version: 1.0.0
---

# Factory Method Pattern

## What Is It
The Factory Method pattern defines an interface for creating objects but lets subclasses decide which class to instantiate. It delegates instantiation logic to child classes, promoting loose coupling between creator and product classes.

## When to Use
- **Unknown object types at compile time** - When your code shouldn't know the specific class to create
- **Library/framework development** - When users need to extend your components with custom classes
- **Complex object creation logic** - When object creation involves more than simple instantiation
- **Runtime polymorphic creation** - When the concrete type depends on runtime conditions
- **Testing with mock objects** - Easy to substitute implementations for testing
- **Multiple related product variants** - When you have families of related products

## When NOT to Use
- **Simple object creation** - If `new ClassName()` suffices, don't over-engineer
- **Only one product type** - No need for factory pattern if there's no variation
- **Static creation logic** - When creation logic never changes
- **Performance-critical paths** - Factory indirection adds minimal overhead

## How to Implement

### Implementation Steps
1. Define a common product interface that all products will implement
2. Create concrete product classes implementing the interface
3. Define an abstract creator class with an abstract factory method
4. Create concrete creator subclasses that implement the factory method
5. Client code uses the creator interface, not concrete creators

### TypeScript Implementation

```typescript
// Product interface
interface Product {
  operation(): string;
}

// Concrete products
class ConcreteProductA implements Product {
  operation(): string {
    return 'Product A operation';
  }
}

class ConcreteProductB implements Product {
  operation(): string {
    return 'Product B operation';
  }
}

// Creator abstract class
abstract class Creator {
  // Factory method
  public abstract factoryMethod(): Product;

  public someOperation(): string {
    const product = this.factoryMethod();
    return `Creator: ${product.operation()}`;
  }
}

// Concrete creators
class ConcreteCreatorA extends Creator {
  public factoryMethod(): Product {
    return new ConcreteProductA();
  }
}

class ConcreteCreatorB extends Creator {
  public factoryMethod(): Product {
    return new ConcreteProductB();
  }
}

// Usage
function clientCode(creator: Creator) {
  console.log(creator.someOperation());
}

clientCode(new ConcreteCreatorA());
clientCode(new ConcreteCreatorB());
```

## Code Examples
See `examples/` directory for runnable TypeScript implementations:
- `examples/factory-method.ts` - Basic implementation with document processing
- `examples/factory-method-advanced.ts` - Real-world example with payment processing

## Related Patterns
- **Abstract Factory** - Often implemented with Factory Methods
- **Template Method** - Factory Method is a specialization of Template Method
- **Prototype** - No subclassing needed; Prototype creates objects by cloning
- **Singleton** - Factory Methods in concrete creators often return singletons
