Bird
Raised Fist0

Which pattern allows you to change an object's behavior at runtime by switching between different algorithms or strategies?

easy🧠 Conceptual Q12 of Q15
LLD - Behavioral Design Patterns — Part 2
Which pattern allows you to change an object's behavior at runtime by switching between different algorithms or strategies?
AObserver pattern
BStrategy pattern
CCommand pattern
DChain of Responsibility pattern
Step-by-Step Solution
Solution:
  1. Step 1: Identify the need for interchangeable behaviors

    The question asks about changing behavior dynamically, which means selecting algorithms or methods at runtime.
  2. Step 2: Select the pattern that supports behavior switching

    The Strategy pattern encapsulates algorithms and lets you swap them easily without changing the client code.
  3. Final Answer:

    Strategy pattern -> Option B
  4. Quick Check:

    Change behavior dynamically = Strategy [OK]
Quick Trick: Switch algorithms easily? Use Strategy pattern [OK]
Common Mistakes:
MISTAKES
  • Mixing Strategy with Observer
  • Using Command for behavior changes
  • Choosing Chain of Responsibility incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes