Bird
Raised Fist0

What is the best follow-up design approach to maintain OCP compliance?

hard🎤 Interviewer Follow-up Q10 of Q15
OOP & Design Patterns - Open/Closed Principle - Open for Extension, Closed for Modification
After implementing a plugin system following the Open/Closed Principle, you discover that some plugins require modifying core system behavior. What is the best follow-up design approach to maintain OCP compliance?
AIntroduce extension points and hooks in the core system to delegate behavior to plugins.
BAllow plugins to override core classes directly to customize behavior.
CRequire all plugins to submit patches modifying core code for approval.
DDisable plugins that need to modify core behavior to enforce strict OCP.
Step-by-Step Solution
Solution:
  1. Step 1: Identify OCP-compliant extension

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

    Allow plugins to override core classes directly to customize behavior breaks encapsulation and OCP by allowing direct overrides. Introduce extension points and hooks in the core system to delegate behavior to plugins introduces extension points/hooks, enabling plugins to extend behavior without modifying core. Require all plugins to submit patches modifying core code for approval violates OCP by requiring core modifications. Disable plugins that need to modify core behavior to enforce strict OCP is impractical and limits extensibility.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Extension points allow behavior changes without core modification [OK]
Quick Trick: Use hooks/extension points to delegate behavior, not modify core [OK]
Common Mistakes:
MISTAKES
  • Allowing direct core overrides
  • Forcing core code patches
Trap Explanation:
PITFALL
  • Candidates may choose direct overrides or patches, violating OCP and risking instability.
Interviewer Note:
CONTEXT
  • Evaluates advanced OCP application and extensibility design.
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