Skip to content

Latest commit

 

History

History
34 lines (23 loc) · 1.61 KB

File metadata and controls

34 lines (23 loc) · 1.61 KB

Open-Closed Principle

Building Software that Adapts without Breaking

In essence, the Open-Closed Principle empowers you to build software that can seamlessly adapt and grow over time, just like a well-designed house that allows you to add new rooms and windows without compromising its sturdiness and comfort.


How OCP works

  1. Design your code around abstractions, not concretions. Focus on interfaces and abstract classes that define expected behaviors, not specific implementations.
  2. Clients rely on the abstractions, not the concrete implementations. Code using the abstractions can be extended without knowing the specifics of how functionalities are implemented.
  3. New functionalities are implemented through extension of existing abstractions. New concrete classes are created that adhere to the existing abstractions, providing additional functionalities without modifying the core code.

Benefits

  • Increased flexibility Easily add new features and functionalities without touching existing code.
  • Improved maintainability Modifying core functionalities doesn't affect extended code, making updates easier.
  • Enhanced reusability Abstractions promote code reuse across different functionalities and implementations.
  • Resilient to change New requirements can be accommodated without extensive code refactoring.

Remember

  • Use OCP when you anticipate future need for additional functionalities within existing code.
  • Overuse can lead to unnecessary abstraction and complexity for simple situations.
  • Choose clear and descriptive names for abstractions and their functionalities.