Open closed principle is one of the most important consideration for implementing SOLID principles. It has to be consider before we design any software system. Here in this article I would like to discuss what it is and how we can use it in designing software.

Let’s start with the actual principle statement. As per wikipedia

In object-oriented programming, the open/closed principle states “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification”; that is, such an entity can allow its behaviour to be extended without modifying its source code.

Putting it in simple word it says we should be able to enhance more functionality to the class without touching the source code at all.

In real life software world we have to model many entities. At the design time we don’t know when our software is implemented how exactly it is used. But certain things we are sure. One good example of Mortgage industry. Say we are designing software for Lenders who provides loan to the people. There can be many types of loan and loan types keep changing the way we can’t predict. There can be personal loan, there can housing loan, there cab gold loan or there can be student loan. However, certain things are we know that will never change. Say for example each loan has loan amount, rate of interest and borrower associated with it. However we can’t predict other criteria like which are specific to loans and it varies based on the loan type itself.

To understand it better I will try here to model this scenario with two approach. First approach is simple and not correct which violate the open closed principle. We will see how difficult it is to change when bank introduce new loans. How much testing it requires and how the quality of your system will go for toss. Second approach will use open closed principle. It will show you how easily you can implement the new loan type into system and make your code quality better.

  

namespace SolidDemo
{
    public class WithoutUsingSolidLoan
    {
        public double LoanAmount { get; set; }
        public double RateOfInterest { get; set; }
        public LoanType MyLoanType { get; set; }
        public enum LoanType { CarLoan, GoldLoan }
        public double GetProcessingCharge()
        {
            if (this.MyLoanType == LoanType.GoldLoan)
            {
                return (LoanAmount * 1) / 100;
            }
            else if (this.MyLoanType == LoanType.CarLoan)
            {
                return (LoanAmount * 2) / 100;
            }
            else
            {
                return (LoanAmount * 0.5) / 100;
            }
        }
    }
}

Right Approach it to use open closed principle as below.

  
using System;

namespace SolidDemo
{
    public class Loan
    {
        public double LoanAmount { get; set; }
        public double RateOfInterest { get; set; }
        public virtual double GetProcessingCharge()
        {
            return (LoanAmount * 1) / 100;
        }
    }
    public class GoldLoan : Loan
    {

        public override double GetProcessingCharge()
        {
            return (LoanAmount * 2) / 100;
        }
    }
    public class CarLoan : Loan
    {
        public override double GetProcessingCharge()
        {
            return (LoanAmount * 2) / 100 + 2000;
        }
    }

}

Please follow and like us:
Open closed Principle with Simple Example

Leave a Reply

Your email address will not be published. Required fields are marked *