Skip to main content

SOLID - Open Closed - Principle


The 'O' in SOLID stand for Open Closed Principle, the definition based on Wikipedia is

"software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification".

This term is originated from Bertrand Meyer in his book 'Object-Oriented Software Construction' ( 1988 ).

Later the understanding of this principle has been elaborate more deeper by Robert C. Martin and he written in his book that

"Open for extension." This means that the behavior of the module can be extended. As the requirements of the application change, we are able to extend the module with new behaviors that satisfy those changes. In other words, we are able to change what the module does. "Closed for modiication." Extending the behavior of a module does not result in changes to the source or binary code of the module. The binary executable version of the module, whether in a linkable library, a DLL, or a Java .jar, remains untouched.

Both ideas are what SOLID principles trying to achieve. All developers must be able to respond to rapid requirement changes and deliver new features. This must be completed with the fact that some modules being closed to modification. Developers must support new functionality without editing the source code—or compiled assembly—of the existing modules.

Open Closed Principle is all about changes and modifications, There are some exceptions on type of changes, the acceptable source of changes may come in 2 ways .

1. Bug and Fixes / Defects.
2. Client Awareness.

1. Bug and Fixes / Defects.
Bugs appear in all software and is a common problem, it is almost impossible to prevent it entirely. When a bug is found, it is a must for a modification.
In order to handle the bugs in our source code, it is advisable to create a unit/integration testing that will target the bug. This is useful in a long term, so that the bug will not be reproduce.

2. Client Awareness.
One more exception changes that is allowed in the Open Closed Principle is that "any change is allowed to existing code as long as it does not also require a change to any client of that code". Gary McLean Hall ( 2014 ) in his book ( Adaptive Coding in C# ). It is always important to design our classes to be loosely coupled, in other word that if a changes made to a specific class, the client that is referencing to the class will not be affected or changed. Vice versa with a tightly coupled, where a changes at a class will force some modification to the other classes that is referencing to it.

With both understanding above, we may now proceed to the "Open for Extension" and how to implement it. There are various ways that we may open our source code for extension.

1. Virtual Method.
2. Partial-ling Our Classes.
3. Abstracting Our Classes.
4. Interfacing Our Classes.


Published on : 10-Jan-2019
Ref no : DTC-WPUB-000099

About Author

My photo
Wan Mohd Adzha CAPM,MCPD,MCSD,MCSE
Passionate about new technology ( Software Engineering ) and how to build,manage and maintain them

Comments