Bird
Raised Fist0

You are designing a plugin system where plugins can be added or removed at runtime, each providing different behaviors to a core application. Which approach is better and why?

hard⚖️ Approach Comparison Q8 of Q15
OOP & Design Patterns - Composition vs Inheritance - Favour Composition, Why & When
You are designing a plugin system where plugins can be added or removed at runtime, each providing different behaviors to a core application. Which approach is better and why?
AComposition, because plugins can be composed dynamically with the core application.
BGlobal variables to track plugin states, because they centralize control.
CStatic methods in utility classes, because they simplify plugin invocation.
DInheritance, because plugins can subclass the core application to add behaviors.
Step-by-Step Solution
Solution:
  1. Step 1: Understand runtime behavior changes

    Plugins must be added/removed dynamically without recompilation.
  2. Step 2: Evaluate inheritance limitations

    Inheritance is static and fixed at compile time, unsuitable for dynamic plugins.
  3. Step 3: Composition supports dynamic behavior

    Composition allows attaching/detaching plugin objects at runtime, enabling flexibility.
  4. Step 4: Assess other options

    Static methods and global variables reduce modularity and encapsulation, increasing coupling.
  5. Final Answer:

    Option A -> Option A
  6. Quick Check:

    Composition supports dynamic plugin management better than inheritance [OK]
Quick Trick: Dynamic behavior favors composition over static inheritance [OK]
Common Mistakes:
MISTAKES
  • Assuming inheritance supports runtime behavior changes
  • Using global state instead of modular design
Trap Explanation:
PITFALL
  • Candidates often pick inheritance due to familiarity, ignoring dynamic flexibility needs.
Interviewer Note:
CONTEXT
  • Tests application of composition in dynamic, modular systems.
Master "Composition vs Inheritance - Favour Composition, Why & When" 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