| Users | Changes in System |
|---|---|
| 100 users | Simple inheritance and method overrides work well. Few subclasses needed. Low complexity. |
| 10,000 users | More subclasses created for variations. Code reuse helps maintainability. Performance still good. |
| 1,000,000 users | Many subclasses cause code bloat. Template methods may become complex. Need to optimize method calls and reduce duplication. |
| 100,000,000 users | System complexity high. Template method pattern alone insufficient. Need to combine with other patterns (e.g., Strategy) and optimize for concurrency and scalability. |
Template Method pattern in LLD - Scalability & System Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
The first bottleneck is code complexity and maintainability as the number of subclasses grows. This leads to harder debugging and slower development.
- Refactor common steps into shared helper classes to reduce subclass count.
- Use composition with Strategy pattern to replace deep inheritance hierarchies.
- Apply caching for repeated method results to improve performance.
- Optimize method calls by minimizing overrides and using final methods where possible.
- For concurrency, ensure thread-safe implementations of template methods.
At 1,000 users, method call overhead is negligible.
At 1,000,000 users, frequent method calls in deep inheritance can add latency (milliseconds per request).
Storage cost grows with number of subclasses and code size, but usually minimal compared to runtime costs.
Bandwidth is unaffected by pattern choice.
Start by explaining the pattern's purpose: defining a skeleton of an algorithm with steps overridden by subclasses.
Discuss how it helps code reuse and enforces a fixed process.
Then analyze scalability: what happens when many subclasses exist and how complexity grows.
Finally, suggest improvements like composition and caching to handle scale.
Your database handles 1000 QPS. Traffic grows 10x. What do you do first?
Answer: Although this question is about database, for Template Method pattern scaling, first address code complexity by refactoring and using composition to keep the system maintainable and performant.
Practice
Template Method pattern in system design?Solution
Step 1: Understand the Template Method pattern goal
The pattern fixes the overall process flow but allows subclasses to customize certain steps.Step 2: Analyze each option
To define a fixed sequence of steps with some customizable parts correctly describes this fixed sequence with customizable parts. Options A, B, and C do not match the pattern's intent.Final Answer:
To define a fixed sequence of steps with some customizable parts -> Option BQuick Check:
Template Method = fixed process + flexible steps [OK]
- Thinking subclasses rewrite the whole process
- Confusing Template Method with Strategy pattern
- Believing the pattern removes all common code
- Assuming no steps are customizable
Solution
Step 1: Identify the structure of a template method
A template method is a method that defines the sequence of steps by calling other methods in order.Step 2: Evaluate each option
Define a method that calls other methods in a fixed order matches this definition. Options B, C, and D do not follow the fixed sequence concept.Final Answer:
Define a method that calls other methods in a fixed order -> Option CQuick Check:
Template method = fixed calls order [OK]
- Not calling methods in a fixed sequence
- Overriding template method instead of steps
- Ignoring the fixed process structure
- Using random or unordered calls
class Game:
def play(self):
self.start()
self.play_turn()
self.end()
def start(self):
print('Game started')
def play_turn(self):
print('Playing turn')
def end(self):
print('Game ended')
class Chess(Game):
def play_turn(self):
print('Chess turn played')
chess = Chess()
chess.play()What will be the output when
chess.play() is called?Solution
Step 1: Trace the play() method calls
The play() method calls start(), play_turn(), and end() in order.Step 2: Identify overridden methods
Chess overrides play_turn(), so Chess's version prints 'Chess turn played'. start() and end() use base class prints.Final Answer:
Game started Chess turn played Game ended -> Option DQuick Check:
Template calls base start/end + overridden play_turn [OK]
- Ignoring method overriding
- Assuming base play_turn() runs
- Mixing order of prints
- Thinking play() is overridden
class Report:
def generate(self):
self.load_data()
self.process_data()
self.save_report()
def load_data(self):
print('Loading data')
def process_data(self):
print('Processing data')
def save_report(self):
print('Saving report')
class CustomReport(Report):
def generate(self):
print('Custom generate start')
self.load_data()
self.process_data()
self.save_report()
print('Custom generate end')Solution
Step 1: Identify the template method and overrides
The base class defines generate() as the template method. The subclass overrides generate() itself.Step 2: Understand Template Method pattern rules
In this pattern, subclasses should override steps, not the template method, to keep the fixed process intact.Final Answer:
The subclass overrides the template method instead of steps -> Option AQuick Check:
Template method must not be overridden [OK]
- Overriding the whole template method
- Ignoring base class method definitions
- Assuming private methods cause issues
- Thinking subclass must call base generate() explicitly
Solution
Step 1: Identify fixed and customizable parts
The sequence (open, parse, validate, save, close) is fixed; parse and validate vary by document type.Step 2: Apply Template Method pattern correctly
Create a base class with a template method that calls all steps in order. Subclasses override parse and validate to customize behavior.Final Answer:
Create a base class with a template method calling open, parse, validate, save, close; subclasses override parse and validate -> Option AQuick Check:
Fixed sequence + customizable steps = Template Method [OK]
- Not fixing the sequence in one place
- Forcing subclasses to rewrite entire process
- Ignoring the need for a template method
- Separating steps without order control
