Open/Closed Principle (OCP) | SOLID PRINCIPLES

Open/Closed Principle (OCP) 

The Open/Closed Principle (OCP) states that a class should be open for extension but closed for modification. This means that the behavior of a class can be extended without modifying its source code. 

Example in C#:


Violation of OCP:


Suppose you have a class that calculates areas of different shapes:


public class AreaCalculator

{

    public double CalculateRectangleArea(double length, double width)

    {

        return length * width;

    }


    public double CalculateCircleArea(double radius)

    {

        return Math.PI * Math.Pow(radius, 2);

    }

}



Now, if you want to add a new shape, like a triangle, you would need to modify the existing class, violating the Open/Closed Principle.


**Adhering to OCP:**


Refactor the code by introducing an interface and creating separate classes for each shape:



public interface IShape

{

    double CalculateArea();

}


public class Rectangle : IShape

{

    private double Length { get; set; }

    private double Width { get; set; }


    public Rectangle(double length, double width)

    {

        Length = length;

        Width = width;

    }


    public double CalculateArea()

    {

        return Length * Width;

    }

}


public class Circle : IShape

{

    private double Radius { get; set; }


    public Circle(double radius)

    {

        Radius = radius;

    }


    public double CalculateArea()

    {

        return Math.PI * Math.Pow(Radius, 2);

    }

}



Now, you can easily add new shapes by creating classes that implement the `IShape` interface without modifying the existing code. This adheres to the Open/Closed Principle, promoting code extensibility and maintainability.


Real-world example related to a system that processes different types of orders.


**Violation of OCP:**


Suppose you have an `OrderProcessor` class that handles orders of different types:


public class OrderProcessor

{

    public void ProcessOrder(Order order)

    {

        if (order.Type == OrderType.Standard)

        {

            // Process standard order

        }

        else if (order.Type == OrderType.Express)

        {

            // Process express order

        }

        // More conditions for other order types...

    }

}



If a new order type is introduced, say `VIP`, you would need to modify the existing `OrderProcessor` class, violating the Open/Closed Principle.


**Adhering to OCP:**


Refactor the code by introducing an interface (`IOrderProcessor`) and creating separate classes for each order type:



public interface IOrderProcessor

{

    void ProcessOrder(Order order);

}


public class StandardOrderProcessor : IOrderProcessor

{

    public void ProcessOrder(Order order)

    {

        // Process standard order

    }

}


public class ExpressOrderProcessor : IOrderProcessor

{

    public void ProcessOrder(Order order)

    {

        // Process express order

    }

}


// Additional classes for new order types (e.g., VIPOrderProcessor)...



Now, you can easily add new order types by creating classes that implement the `IOrderProcessor` interface without modifying the existing code. This adheres to the Open/Closed Principle, allowing for easy extension and avoiding the need to modify existing code when introducing new order types.




Comments