TypeScript Factory Design Pattern with Example

1. Definition

The Factory Method pattern is a creational design pattern that provides an interface for creating objects in a super class, but allows subclasses to alter the types of objects that will be created.

2. Problem Statement

Imagine having a class that requires the creation of several types of objects but doesn't want to specify the exact classes of the objects that will be created. Direct instantiation can lead to a tight coupling between classes, making the system less modular and harder to extend.

3. Solution

The Factory Method pattern solves this problem by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created.

4. Real-World Use Cases

1. GUI libraries where each OS provides a different implementation of a button or a window.

2. E-commerce platforms where different types of memberships might have different privileges or discount calculations.

3. Payment gateway integrations where each provider has a different method to process a payment.

5. Implementation Steps

1. Create an abstract class (or interface) with an abstract factory method.

2. Concrete subclasses will implement this factory method to produce objects that conform to the desired product interface.

6. Implementation in TypeScript

// Step 1: Create an abstract product interface
interface Product {
    getDescription(): string;
}
// Step 2: Create concrete products
class ConcreteProductA implements Product {
    getDescription(): string {
        return "Description of Product A";
    }
}
class ConcreteProductB implements Product {
    getDescription(): string {
        return "Description of Product B";
    }
}
// Step 3: Create an abstract creator class with the factory method
abstract class Creator {
    public abstract factoryMethod(): Product;
    public someOperation(): string {
        const product = this.factoryMethod();
        return `Creator: ${product.getDescription()}`;
    }
}
// Step 4: Create concrete creators
class ConcreteCreatorA extends Creator {
    factoryMethod(): Product {
        return new ConcreteProductA();
    }
}
class ConcreteCreatorB extends Creator {
    factoryMethod(): Product {
        return new ConcreteProductB();
    }
}
// Usage:
const creatorA = new ConcreteCreatorA();
const creatorB = new ConcreteCreatorB();
console.log(creatorA.someOperation());  // Outputs: Creator: Description of Product A
console.log(creatorB.someOperation());  // Outputs: Creator: Description of Product B

Output:

Creator: Description of Product A
Creator: Description of Product B

Explanation:

The Factory Method pattern separates the method of creating objects from the main class, allowing subclasses to decide which class to instantiate. 

In the TypeScript implementation, the Creator class has a factory method that returns a Product

The concrete creators (ConcreteCreatorA and ConcreteCreatorB) override this factory method to produce different types of products. This ensures that the creation of objects is decoupled from the main logic, offering more flexibility and easier maintenance.

7. When to use?

The Factory Method pattern is applicable when:

1. A class cannot anticipate the type of objects it is supposed to create.

2. A class wants its subclasses to specify the objects it creates.

3. The process of creating an object should be independent of the main system, promoting modularity and scalability.


Comments