Bird
0
0

How should you apply the Template Method pattern here?

hard📝 Trade-off Q15 of 15
LLD - Behavioral Design Patterns — Part 1
You are designing a document processing system where each document type requires a specific sequence of steps: open, parse, validate, save, and close. You want to ensure the sequence is fixed but allow each document type to customize parsing and validation. How should you apply the Template Method pattern here?
ACreate a base class with a template method calling open, parse, validate, save, close; subclasses override parse and validate
BLet each subclass implement its own full process without a base class
CUse separate unrelated classes for each step without a fixed sequence
DMake all steps abstract and force subclasses to implement the entire process
Step-by-Step Solution
Solution:
  1. Step 1: Identify fixed and customizable parts

    The sequence (open, parse, validate, save, close) is fixed; parse and validate vary by document type.
  2. 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.
  3. Final Answer:

    Create a base class with a template method calling open, parse, validate, save, close; subclasses override parse and validate -> Option A
  4. Quick Check:

    Fixed sequence + customizable steps = Template Method [OK]
Quick Trick: Fix sequence in base; override variable steps in subclasses [OK]
Common Mistakes:
MISTAKES
  • 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes