The open/closed principle (OCP)

The Open/Closed Principle (OCP) is a principle of object-oriented programming that states that a class or module should be open for extension but closed for modification. This means that a class or module should be designed in such a way that new functionality can be added without modifying the existing code.

Coffee machine need update

Image a coffe machine that make expresso, the code could be like this :

class CoffeeMachine
{
    public void MakeEspresso()
    {
        Console.WriteLine("Making an Espresso.");
    }
}

class Client
{
    static void Main(string[] args)
    {
        CoffeeMachine cm = new CoffeeMachine();
        cm.MakeEspresso();
    }
}

Someday you have to improve your machine to also do Americano, and you innocently add a new method « MakeAmericano » :

class CoffeeMachine
{
    public void MakeEspresso()
    {
        Console.WriteLine("Making an Espresso.");
    }

    public void MakeAmericano()
    {
        Console.WriteLine("Making an Americano.");
    }
}

class Client
{
    static void Main(string[] args)
    {
        CoffeeMachine cm = new CoffeeMachine();
        cm.MakeEspresso();
        cm.MakeAmericano();
    }
}

In this example, the coffee machine class is closed for extension (new types of coffee cannot be added without modifying the existing code) because new types of coffee must be added as new methods in the CoffeeMachine class, and is open for modification (the existing code must be modified) because the CoffeeMachine class must be modified to add support for new types of coffee.

This implementation violates the OCP, because adding new functionality will require modification to the existing code, which will lead to potential bugs and maintenance issues in the long run.

Don’t touch my coffe machine, extend it

The coffee machine class should have a method for making coffee, but it should be designed in such a way that new types of coffee can be added without modifying the existing code.

Here’s an example of a simple implementation of a coffee machine class that follows the OCP:

interface ICoffee
{
    void Make();
}

class Espresso : ICoffee
{
    public void Make()
    {
        Console.WriteLine("Making an Espresso.");
    }
}

class Americano : ICoffee
{
    public void Make()
    {
        Console.WriteLine("Making an Americano.");
    }
}

class CoffeeMachine
{
    private ICoffee _coffee;

    public CoffeeMachine(ICoffee coffee)
    {
        _coffee = coffee;
    }

    public void MakeCoffee()
    {
        _coffee.Make();
    }
}

class Client
{
    static void Main(string[] args)
    {
        CoffeeMachine cm = new CoffeeMachine(new Espresso());
        cm.MakeCoffee();
        cm = new CoffeeMachine(new Americano());
        cm.MakeCoffee();
    }
}

This simple example, the coffee machine class is open for extension (new types of coffee can be added without modifying the existing code) because new types of coffee can be added as a new class that implements the ICoffee interface, and closed for modification (the existing code does not need to be modified) because the MakeCoffee method does not need to be modified to support new types of coffee

The Open/Closed Principle is used to promote maintainable, flexible, and extensible code.

More coffee ?

Here are some benefits of using the OCP:

Easier maintenance: By following the OCP, classes and modules are designed in such a way that new functionality can be added without modifying the existing code. This makes it easier to maintain the codebase over time, as changes to one part of the system do not affect other parts of the system.

Reduced risk of bugs: When code is closed for modification, it becomes less likely that changes to the codebase will introduce bugs. Because new functionality is added through extension rather than modification, the existing code is less likely to be affected by the changes.

Easier to test: Classes and modules that follow the OCP are typically easier to test, as they have a clear, well-defined interface that can be easily mocked or stubbed out during testing.

Overall, OCP is a principle that helps to ensure that code is maintainable, flexible, and extensible over time, which can help to reduce development costs and improve the overall quality of the codebase.

2 réponses à “The open/closed principle (OCP)”

  1. […] methods align with the Open/Closed Principle by allowing the extension of a class’s behavior without modifying its source code. This […]

  2. […] design pattern can be a solution to adhere to the SOLID principles of Single Responsibility and Open/Closed. To achieve this, organize the code related to different states into separate classes while adding […]

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *