Bird
0
0

In the following code, what is the main problem that breaks the Template Method pattern?

medium📝 Analysis Q14 of 15
LLD - Behavioral Design Patterns — Part 1
In the following code, what is the main problem that breaks the Template Method pattern?
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')
AThe subclass overrides the template method instead of steps
BThe base class does not define any methods
CThe subclass does not call any base methods
DThe base class methods are private
Step-by-Step Solution
Solution:
  1. Step 1: Identify the template method and overrides

    The base class defines generate() as the template method. The subclass overrides generate() itself.
  2. Step 2: Understand Template Method pattern rules

    In this pattern, subclasses should override steps, not the template method, to keep the fixed process intact.
  3. Final Answer:

    The subclass overrides the template method instead of steps -> Option A
  4. Quick Check:

    Template method must not be overridden [OK]
Quick Trick: Override steps, not the template method [OK]
Common Mistakes:
MISTAKES
  • Overriding the whole template method
  • Ignoring base class method definitions
  • Assuming private methods cause issues
  • Thinking subclass must call base generate() explicitly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes