NOTE
Open–Closed Principle
What the open–closed principle is, why it is needed, and how to implement it through abstractions, inheritance, composition, and aggregation.
This is a historical learning note and may contain outdated or incomplete understanding.
1. What Is the Open–Closed Principle?
Open for extension, closed for modification.
2. Why Is the Open–Closed Principle Needed?
Reduce new risks introduced by maintenance.
3. Implementing the Open–Closed Principle
When modifying code for a new requirement, check whether existing code needs to be modified. If so, refactor it: define an abstraction layer through interfaces or abstract classes, and encapsulate variable factors in concrete implementation classes.
Inheritance: Liskov Substitution Principle.md
Composition/aggregation: Composition Reuse Principle.md
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub