Bird
Raised Fist0

If a class in the LLD violates the 'open-closed principle', what is the MOST appropriate corrective measure?

medium๐Ÿ“ Analysis Q6 of Q15
LLD - Advanced LLD Concepts
If a class in the LLD violates the 'open-closed principle', what is the MOST appropriate corrective measure?
ARefactor the class to allow behavior extension via inheritance or composition without modifying existing code
BMerge the class with other classes to reduce the number of modules
CRemove all public methods to prevent external changes
DRewrite the entire class from scratch using a different programming language
Step-by-Step Solution
Solution:
  1. Step 1: Understand open-closed principle

    It states software entities should be open for extension but closed for modification.
  2. Step 2: Identify fix

    Refactoring to support extension via inheritance or composition achieves this without modifying existing code.
  3. Step 3: Eliminate incorrect options

    Merging classes or removing public methods does not address the principle; rewriting is unnecessary.
  4. Final Answer:

    Refactor the class to allow behavior extension via inheritance or composition without modifying existing code -> Option A
  5. Quick Check:

    Enable extension without changing existing code [OK]
Quick Trick: Refactor for extension without modification [OK]
Common Mistakes:
MISTAKES
  • Thinking rewriting is the only solution
  • Confusing open-closed with encapsulation

Want More Practice?

15+ quiz questions ยท All difficulty levels ยท Free

Free Signup - Practice All Questions
More LLD Quizzes