Tuesday, April 6, 2021

SOLID | Open Closed Principle (OCP)

Open Closed Principle (OCP)

A class can be open for extension and closed for modification. 

Points to remember,  
  • Software needs to be flexible, saying that it should have the ability to accept the various changes
  • OCP helps to make application flexible enough to accept the new behavior rather than changing the old one
  • So new implementation would go in new classes and logic but nothing can be changed in the existing implementation
  • Helps to maintain the code easily without breaking the whole thing
  • The best way to remember is an electric adapter in your wall, once fitted it's close for modification but open for extension using an external adapter. 

Example with initial implementation of Employee class,

public class Employee
{
        public void CalculateSalary(Employee employee)
        {
              if(employee.Type == "Permenent")
                    //Salary calculation and print the same
              else if(employee.Type == "Contract")
                    //Salary calculation and print the same
        }
}

Now, the above class looks great as it has an Employee and it calculates salary based on their type of Permanent and Contract. And because it is not following the open closed principle adding a new EmployeeType would require a whole class to be changed. 

Now, Thinking the same example with Open Closed Principle,

public interface IEmployee
{
        void CalculateSalary(Employee employee);
}

public class PermenentEmployee: IEmployee
{
         public void CalculateSalary(Employee employee)
        {
              //Salary calculation and print the same
        }
}
public class ContractEmployee: IEmployee
{
         public void CalculateSalary(Employee employee)
        {
              //Salary calculation and print the same
        }
}
public class TemporaryEmployee: IEmployee
{
         public void CalculateSalary(Employee employee)
        {
              //Salary calculation and print the same
        }
}

Now, we have created an Interface and whole new classes to have the ability to create new Employee Types rather than changing the class all the time. Modifying existing implementation would not be a good idea at all. 

The interface IEmployee shares a common contract and that can be shared across derived classes. Now in this example also has a loop whole, let me know if you can find it another way. 

Why use the Open Closed principle?    

  • Software needs flexibility to change, and as per the above example if requirements are quite extending the same functionality then we can have a better approach toward the open-closed principle
  • It is also breaking another first princple of SOlID that is Single Responsibility, The above class had a multiple responsibility and we have devided that into their own territory so in future if anything comes up, no modification will be done in existing code
  • Maintenance becomes more convenient   

In conclusion, I would say, Open Closed Principle also requires implementation of Single Responsibility Principle, Breaking responsibilities from a single entity, and making a framework in a way that is open for extension and closed for modification meets the goal. 

No comments:

Post a Comment

C# Interview Prep: 100 Common Questions for 0-3 Years Experience

Common C# Interview Questions  Basics of C# Language: What is C#? C# (pronounced C sharp) is a modern, object-oriented programming language...