| Users | Objects Created | Complexity | Impact on System |
|---|---|---|---|
| 100 users | Low (hundreds) | Simple | Factory handles creation easily, no performance issues |
| 10,000 users | Moderate (thousands) | Manageable | Factory method may need optimization, caching objects helps |
| 1,000,000 users | High (millions) | Complex | Object creation cost adds up, factory needs pooling or reuse |
| 100,000,000 users | Very High (hundreds of millions) | Very Complex | Factory method alone insufficient; must combine with caching, pooling, and distributed creation |
Factory Method pattern in LLD - Scalability & System Analysis
The first bottleneck is the object creation overhead in the factory method. As user count grows, creating many objects repeatedly consumes CPU and memory, slowing down the system.
- Object Pooling: Reuse objects instead of creating new ones each time to reduce CPU and memory load.
- Caching: Cache created objects when possible to avoid repeated creation.
- Lazy Initialization: Create objects only when needed, not upfront.
- Horizontal Scaling: Distribute factory instances across multiple servers to share load.
- Asynchronous Creation: Create objects in background threads to avoid blocking main flow.
Assuming each user triggers 1 object creation per second:
- At 1,000 users: 1,000 creations/sec, manageable by a single server.
- At 1,000,000 users: 1 million creations/sec, requires multiple servers and pooling.
- Memory per object ~1KB, so 1 million objects = ~1GB memory usage if all live simultaneously.
- CPU usage grows linearly with creation rate; pooling reduces CPU by reusing objects.
Start by explaining what the Factory Method pattern does: it creates objects without specifying exact classes. Then discuss how object creation cost grows with users. Identify bottlenecks like CPU and memory. Suggest solutions like pooling and caching. Finally, mention horizontal scaling and asynchronous creation to handle very high loads.
Your database handles 1000 QPS. Traffic grows 10x. What do you do first?
Answer: Since the database is the bottleneck, first add read replicas or caching to reduce load. For Factory Method pattern, similarly, first optimize object creation by adding pooling or caching before scaling horizontally.
Practice
Factory Method pattern in system design?Solution
Step 1: Understand the Factory Method pattern goal
The Factory Method pattern aims to separate object creation from usage by defining an interface for creating objects.Step 2: Identify the role of subclasses
Subclasses decide which specific class to instantiate, allowing flexibility and easy extension.Final Answer:
To define an interface for creating objects but let subclasses decide which class to instantiate -> Option CQuick Check:
Factory Method = Interface for object creation + subclass decides [OK]
- Confusing Factory Method with Singleton pattern
- Thinking Factory Method creates objects directly without abstraction
- Mixing Factory Method with global access patterns
Solution
Step 1: Understand Factory Method signature
The Factory Method returns an object of a product type, so it must have a return type matching the product interface or class.Step 2: Check access and static modifiers
Factory Methods are usually instance methods (not static) and public to allow subclass overriding and usage.Final Answer:
public Product createProduct(); -> Option AQuick Check:
Factory Method returns product instance, public and non-static [OK]
- Using void return type instead of product
- Making Factory Method static which breaks subclass override
- Declaring Factory Method private so subclasses can't override
abstract class Creator {
abstract Product factoryMethod();
void someOperation() {
Product product = factoryMethod();
print(product.operation());
}
}
class ConcreteProductA implements Product {
String operation() => "Product A operation";
}
class ConcreteCreatorA extends Creator {
Product factoryMethod() => ConcreteProductA();
}
void main() {
Creator creator = ConcreteCreatorA();
creator.someOperation();
}Solution
Step 1: Trace the factoryMethod call in ConcreteCreatorA
ConcreteCreatorA overrides factoryMethod to return ConcreteProductA instance.Step 2: Call operation() on ConcreteProductA instance
ConcreteProductA's operation() returns "Product A operation" which is printed.Final Answer:
Product A operation -> Option DQuick Check:
ConcreteCreatorA creates ConcreteProductA, prints its operation [OK]
- Assuming abstract Creator can be instantiated
- Forgetting ConcreteCreatorA overrides factoryMethod
- Expecting generic Product operation output
abstract class Creator {
Product factoryMethod();
}
class ConcreteCreator extends Creator {
void factoryMethod() {
print("Creating product");
}
}Solution
Step 1: Check method signature in abstract Creator
factoryMethod returns Product type, so subclass must match return type.Step 2: Check ConcreteCreator's factoryMethod signature
It returns void, which conflicts with abstract method's Product return type.Final Answer:
ConcreteCreator's factoryMethod has wrong return type -> Option AQuick Check:
Return type mismatch causes error [OK]
- Ignoring return type mismatch
- Thinking abstract class can't have abstract methods
- Assuming static needed for Factory Method
Solution
Step 1: Apply Factory Method pattern structure
Define an abstract creator class with a factoryMethod that returns a Notification interface or abstract class.Step 2: Implement concrete creators for each notification type
Each subclass (EmailCreator, SMSCreator, PushCreator) overrides factoryMethod to create its specific Notification object.Step 3: Ensure easy extension without modifying existing code
Adding new notification types means adding new creator subclasses without changing existing ones, following open-closed principle.Final Answer:
Create an abstract NotificationCreator with a factoryMethod returning Notification; implement subclasses EmailCreator, SMSCreator, PushCreator each creating respective Notification objects -> Option BQuick Check:
Factory Method enables easy extension via subclassing [OK]
- Using if-else inside one class, violating open-closed principle
- Using static methods, reducing flexibility
- Not using a common interface for notifications
Ready to prove it?
Pass the test and this pattern moves unknown
