Tuesday, 7 July 2020

SOLID Design Principles in C# - Open/Closed Principle (OCP)

The open-closed principle definition by Bertrand Meyer

“Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.”

Open for extension: Entities should allow themselves to be extended.

Closed for modification: Once the entities are defined those should not be touched later on.

Once you implemented the base class you should not modify the functionality, if you change it, it will end up changing the behavior of the derived classes. It leads to code changes in all derived classes as well, you will mess up whole derived classes code every time you change the base class. Code maintenance will become difficult, and the code won't be flexible for the further changes.

Open Closed Principle helps in avoid touching the base class and allows the class to extends the functionality of it's by implementing the new class.

Real Life Example:

 - An adapter is closed for modification it provides flexibility to use the ports.

- So you can use the extension board and use it for different purpose.

- You can charge the mobile, laptop, power banks with the extension cables.


Lets take an example of salary processing in a company which sets the max salary based on the designation.

Class which violates Open/Closed Principle

public void GetSalaryLimit(string grade)
{
    if(grade=="A")
    {
        MaxSalary = 2000000;
    }
    else if(grade=="B")
    {
        MaxSalary = 1500000;
    }
    else if(grade=="C")
    {
        MaxSalary = 1200000;
    }
        else if(grade=="D")
    {
        MaxSalary = 1000000;
    }
}

The above code is not following the Open Closed Principle. If the company is introduced the new grades tomorrow we need to make changes in the class. It itself is saying the class is open for the modification. 

Let's fix the code by applying the Open Closed Principle. Inheritance is the only way we can extend the functionality of a class. Lets create an interface ISalaryLimit which will extended by GradeA, GradeB, GradeC, GgradeD employees.

Open/Closed Principle Implementation

public interface ISalaryLimit
{
     void SetMaxSalaryLimit();
}

public class GradeA : ISalaryLimit
{
    public void SetMaxSalaryLimit()
    {
        MaxSalary = 2000000;
    }
}

public class GradeB : ISalaryLimit
{
    public void SetMaxSalaryLimit()
    {
        MaxSalary = 1500000;
    }
}

public class GradeC : ISalaryLimit
{
    public void SetMaxSalaryLimit()
    {
        MaxSalary = 1200000;
    }
}

public class GradeD : ISalaryLimit
{
    public void SetMaxSalaryLimit()
    {
        MaxSalary = 1000000;
    }
}

In the above example we have introduced new Interface which is fixed and from this we extended four classes. If tomorrow company is introduces a new grade we can introduce a new class which extends the ISalaryLimit interface. 

Please check my below posts on other SOLID principles for better understanding

Single Responsibility Principle (SRP)
https://dotnetcookie.blogspot.com/2019/12/solid-design-principles-in-c-single.html



No comments: