Open/Close Principle (OCP)
The Open/Close Principle means that software entities should be open for extension but closed for modification .
Open to extension
“Open to extension” means that classes are designed with the possibility of new functionality in mind. Down the road, one can extend functionality as new requirements come up.
Closed for modification
“Closed for modification” means that once you have developed and tested a class, it should not be modified, except to correct bugs.
High Level “How to” OCP
Rather than using concrete classes, abstractions such as interfaces or abstract classes may be used to extend functionality as the need arises. Then creating new classes that implement the interfaces can extend the functionality. The new classes which implement the interfaces can be tested as units without having to make changes to the original class.
Benefits of OCP
Applying OCP in you object oriented design cuts down on the need to change source code once it has been written. In this way, the “Open to extension Closed for modification” principle reduces the risk of introducing new bugs.
This is a good argument for unit testing. Once each unit has been tested there should be no need to modify it since it’s single responsibility has been proven.
- Open/closed principle, https://en.wikipedia.org/wiki/Open/closed_principle