Bird
Raised Fist0

Which design approach best ensures code reuse and enforces the sequence while allowing subclasses to customize specific steps?

easy🔍 Pattern Recognition Q11 of Q15
OOP & Design Patterns - Template Method Pattern - Define Skeleton, Override Steps
You need to design a system where a fixed sequence of steps is followed to prepare different types of beverages, but some steps vary depending on the beverage type. Which design approach best ensures code reuse and enforces the sequence while allowing subclasses to customize specific steps?
AImplement separate classes with duplicated code for each beverage type.
BApply a greedy algorithm to select steps dynamically at runtime based on beverage type.
CUse a base class defining the skeleton of the algorithm with abstract methods for variable steps, overridden by subclasses.
DUse a brute force approach that tries all possible step sequences and picks the correct one.
Step-by-Step Solution
  1. Step 1: Identify the problem structure

    The problem requires a fixed sequence of steps with some steps varying by subclass.
  2. Step 2: Match to design pattern

    The Template Method Pattern defines a skeleton algorithm in a base class and lets subclasses override specific steps, ensuring reuse and consistent sequence.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Base class controls sequence; subclasses customize steps [OK]
Quick Trick: Fixed sequence with customizable steps -> Template Method [OK]
Common Mistakes:
MISTAKES
  • Thinking duplicated code is acceptable for reuse
  • Confusing dynamic step selection with greedy algorithms
Trap Explanation:
PITFALL
  • Greedy or brute force approaches seem flexible but don't enforce fixed step order or reuse.
Interviewer Note:
CONTEXT
  • Tests if candidate recognizes Template Method pattern from problem description.
Master "Template Method Pattern - Define Skeleton, Override Steps" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes