What is SOLID?

This is a series on the basics of the SOLID principles of software engineering. The SOLID principles were created by Robert C. Martin “Uncle Bob” who is a software engineer public speaker and author.

SOLID is an acrostic that stands for:

There will be an article for each. Lets compare Interface Segregation Principle with Open Close Principle.

Comparing Interface Segregation With Open Close

The open-closed principle (OCP) and the Liskov substitution principle (LSP) are two of the five SOLID principles in object-oriented programming. Like all the SOLID principals these both are important for maintaining the maintainability, extensibility, and overall quality of software systems.

The open-closed principle states that a module should be open for extension but closed for modification. This means that the implementation of a module should be designed in a way that allows new features or changes to be added without having to modify the existing code. This helps to prevent breaking existing functionality and makes the system more flexible and adaptable to changing requirements.

The Liskov substitution principle, on the other hand, states that objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program. This means that if a method is designed to work with an object of a superclass, it should also work with an object of any of its subclasses. This principle is important for ensuring the compatibility and reliability of software systems.

Here’s an example in C# that demonstrates the difference between the open-closed principle and the Liskov substitution principle. Suppose we have a class Employee and two subclasses Manager and Developer. We want to add a new method CalculateBonus() to the Employee class, but the implementation of the method is different for managers and developers:

public class Employee
{
    public virtual void CalculatePay()
    {
        Console.WriteLine("Calculating pay for an employee.");
    }

    public virtual void CalculateBonus()
    {
        Console.WriteLine("Calculating bonus for an employee.");
    }
}

public class Manager : Employee
{
    public override void CalculateBonus()
    {
        Console.WriteLine("Calculating bonus for a manager.");
    }
}

public class Developer : Employee
{
    public override void CalculateBonus()
    {
        Console.WriteLine("Calculating bonus for a developer.");
    }
}

public class Program
{
    public static void Main()
    {
        Employee employee = new Employee();
        employee.CalculatePay();
        employee.CalculateBonus();

        Manager manager = new Manager();
        manager.CalculatePay();
        manager.CalculateBonus();

        Developer developer = new Developer();
        developer.CalculatePay();
        developer.CalculateBonus();
    }
}

In this example, the open-closed principle is being applied because we are able to extend the functionality of the Employee class by adding the CalculateBonus() method without modifying the existing code. The method is implemented differently for managers and developers in their respective subclasses, demonstrating the open-closed principle in action.

The Liskov substitution principle is also being applied because the CalculatePay() method can be called on objects of the Employee class, the Manager class, and the Developer class without affecting the correctness of the program. The CalculatePay() method works with objects of all three classes, demonstrating the Liskov substitution principle in action.

Conclusion

The open-closed principle helps to maintain the maintainability and extensibility of software systems by allowing new features or changes to be added without modifying the existing code. The Liskov substitution principle helps to ensure the compatibility and reliability of software systems by ensuring that objects of a superclass can be replaced with objects of a subclass without affecting the correctness of the program.