This is the “O” in the SOLID Principles of the five basic principles of object orientated design. The official definition is “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.” This means that a developer should be able to make changes to a program without making changes to the classes themselves. This may sound counter intuitive, but perhaps an example will help:
This is a violation of the open and closed principle because we if we add another shape like a rectangle we would need modify
#calculate_area. A better way to write this would be
This way if a developer wants to add another shape with
#calculate_area a new class can be created for each new shape.
This allows the Geometry class to be open for extension, but closed for modification.
Subscribe via RSS