Open Closed Principle


Violation (Bad) Example

HealthInsuranceSurveyor.java

package guru.springframework.blog.openclosedprinciple;
    public class HealthInsuranceSurveyor{
        public boolean isValidClaim(){
            System.out.println("HealthInsuranceSurveyor: Validating health insurance claim...");
            /*Logic to validate health insurance claims*/
            return true;
        }
    }

ClaimApprovalManager.java

package guru.springframework.blog.openclosedprinciple;
    public class ClaimApprovalManager {
        public void processHealthClaim (HealthInsuranceSurveyor surveyor)    {
            if(surveyor.isValidClaim()){
                System.out.println("ClaimApprovalManager: Valid claim. Currently processing claim for approval....");
            }
        }
    }

The above follows the single responsibillity principle, but when a new type of insurance claim in introduced the code has to be modified: : Modified ClaimApprovalManager.java

    package guru.springframework.blog.openclosedprinciple;
    public class ClaimApprovalManager {
        public void processHealthClaim (HealthInsuranceSurveyor surveyor)    {
            if(surveyor.isValidClaim()){
                System.out.println("ClaimApprovalManager: Valid claim. Currently processing claim for approval....");
            }
        }
        public void processVehicleClaim (VehicleInsuranceSurveyor surveyor)    {
            if(surveyor.isValidClaim()){
                System.out.println("ClaimApprovalManager: Valid claim. Currently processing claim for approval....");
            }
        }
    }

Coding to the Open Closed Principal

InsuranceSurveyor.java

    package guru.springframework.blog.openclosedprinciple;
    public abstract class InsuranceSurveyor {
        public abstract boolean isValidClaim();
    }
HealthInsuranceSurveyor.java
    package guru.springframework.blog.openclosedprinciple;
    public class HealthInsuranceSurveyor extends InsuranceSurveyor{
        public boolean isValidClaim(){
            System.out.println("HealthInsuranceSurveyor: Validating health insurance claim...");
            /*Logic to validate health insurance claims*/
            return true;
        }
    }

VehicleInsuranceSurveyor.java

    package guru.springframework.blog.openclosedprinciple;
    public class VehicleInsuranceSurveyor extends InsuranceSurveyor{
        public boolean isValidClaim(){
           System.out.println("VehicleInsuranceSurveyor: Validating vehicle insurance claim...");
            /*Logic to validate vehicle insurance claims*/
            return true;
        }
    }

ClaimApprovalManager.java

    package guru.springframework.blog.openclosedprinciple;
    public class ClaimApprovalManager {
        public void processClaim(InsuranceSurveyor surveyor){
            if(surveyor.isValidClaim()){
                System.out.println("ClaimApprovalManager: Valid claim. Currently processing claim for approval....");
            }
        }
    }   

Our insurance system is now open to support more types of insurance claims, and closed for any modifications whenever a new claim type is added.


Summary

Most of the times real closure of a software entity is practically not possible because there is always a chance that a change will violate the closure. For example, in our insurance example a change in the business rule to process a specific type of claim will require modifying the ClaimApprovalManager class. So, during enterprise application development, even if you might not always manage to write code that satisfies the Open Closed Principle in every aspect, taking the steps towards it will be beneficial as the application evolves.


Revision #2
Created 17 April 2022 00:52:38 by Elkip
Updated 17 April 2022 01:37:44 by Elkip