SOLID: Open Close Principle

Open Close Principle

If you have not read Single Responsibility Principle Start from here Single Responsibility Principle

Classes should be open for extension, but closed for modification

This principle states that any new functionality can be added in a system with minimum modification in the current code. Means the system should be open to additions but it should also closed for modification (or less modification).

A class is closed when it is compiled or shared as library but it is also open since any new class can use it as parent and add new functionality.

The best way to implement Open Close Principle is to start with Single Responsibility Principle (So that each class have only one responsibility) and let the consumer class talk to different classes with abstraction (Interface, So that consumer class need not to know about concrete implementation).

Let’s assume we have an AreaCalculator class which calculates the sum of areas of different shape like this:

So in above example what if we have to add one more Shape? we have to change the AreaCalculator class. So this class doesn’t follow OCP (closed to modification).

Let’s see the following code:


Now, In this code if we have to add a new Shape we just need to create a class and implement the Shape interface. There is no modification needed in AreaCalculator class, so it is closed to modification as well as open to extension.

Next: Likov’s Substitution Principle

Leave a Reply