DEV Community

Cover image for Open/Closed Principle: 2#
Israel-Lopes
Israel-Lopes

Posted on

Open/Closed Principle: 2#

Open/Closed Principle: Software entities (classes, modules, functions, etc.) must be open for extension but closed for modification. This means that you should be able to extend an entity's behavior without modifying its existing source code. This promotes code reuse and modularity.

Let's go to an example of the applicability of this concept.

Suppose we have a system where we have different types of employees, such as RegularEmployee and Manager, and we need to calculate the salary for each of them based on different criteria. Let's start with a simple implementation without considering the Open/Closed Principle:

public class Employee {
    private String name;
    private double salary;

    // Constructor, getters, and setters omitted for simplicity

    public double calculateSalary() {
        return salary;
    }
}

public class Manager extends Employee {
    private double bonus;

    // Constructor, getters, and setters omitted for simplicity

    @Override
    public double calculateSalary() {
        return super.calculateSalary() + bonus;
    }
}

Enter fullscreen mode Exit fullscreen mode

In this case, we have the class Employee as the base and the class Manager as an extension of it. Each class has its own calculateSalary() method, where Manager adds a bonus to the base salary.

However, this implementation does not follow the Open/Closed Principle because if a new type of employee emerges, we would have to modify the existing Employee class. Let's refactor the code to follow the Open/Closed Principle using interfaces and polymorphism:

public interface Employee {
    double calculateSalary();
}

public class RegularEmployee implements Employee {
    private String name;
    private double salary;

    // Constructor, getters, and setters omitted for simplicity

    @Override
    public double calculateSalary() {
        return salary;
    }
}

public class Manager implements Employee {
    private String name;
    private double salary;
    private double bonus;

    // Constructor, getters, and setters omitted for simplicity

    @Override
    public double calculateSalary() {
        return salary + bonus;
    }
}

Enter fullscreen mode Exit fullscreen mode

Here, we introduced the Employee interface that defines the calculateSalary() method. Then, we implemented the RegularEmployee and Manager classes that implement this interface.

Now, if we want to add a new type of employee, such as an Intern, we can create a new class that implements the Employee interface without modifying the existing classes:

public class Intern implements Employee {
    private String name;
    private double stipend;

    // Constructor, getters, and setters omitted for simplicity

    @Override
    public double calculateSalary() {
        return stipend;
    }
}

Enter fullscreen mode Exit fullscreen mode

This way, by following the Open/Closed Principle, we can extend the behavior of the system by adding new types of employees without modifying the existing code.

___________<< back______________next page >>__________

Texto alternativo da imagem

Top comments (0)