Bird
Raised Fist0

Which approach best balances strict Open/Closed Principle adherence with practical extensibility?

hard⚖️ Approach Comparison Q8 of Q15
OOP & Design Patterns - Open/Closed Principle - Open for Extension, Closed for Modification
In a plugin-based architecture, you want to allow third-party developers to add new features without modifying the core system. Which approach best balances strict Open/Closed Principle adherence with practical extensibility?
ADefine stable interfaces and provide extension points via composition and dependency injection.
BExpose internal core classes for direct subclassing by plugins.
CAllow plugins to modify core classes through reflection or monkey patching.
DRequire plugins to fork and modify the core system source code.
Step-by-Step Solution
Solution:
  1. Step 1: Understand OCP in plugin systems

    OCP requires core system to be closed for modification but open for extension.
  2. Step 2: Analyze options

    Expose internal core classes for direct subclassing by plugins risks breaking encapsulation and stability. Define stable interfaces and provide extension points via composition and dependency injection provides stable interfaces and extension points, enabling safe extension without modification. Allow plugins to modify core classes through reflection or monkey patching violates OCP by modifying core classes dynamically. Require plugins to fork and modify the core system source code violates OCP by requiring core modification.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Stable interfaces with composition enable extension without modification [OK]
Quick Trick: Use interfaces and composition for safe extension [OK]
Common Mistakes:
MISTAKES
  • Allowing direct subclassing of core classes
  • Permitting runtime modification of core code
Trap Explanation:
PITFALL
  • Candidates may choose direct subclassing or runtime modification, which break OCP and system stability.
Interviewer Note:
CONTEXT
  • Tests application of OCP in complex extensible systems.
Master "Open/Closed Principle - Open for Extension, Closed for Modification" 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