S.O.L.I.D Principles for beginners (Part 4)

Before you start reading this blog post, please read Part 1Part 2 and Part 3 of the series, if you have not read it.

Interface Segregation Principle
Definition: A class should not forced to depend interface that they do not use.

Let us take the same example we mentioned for Liskov Substitution Principle. In the example. We have 2 interfaces to define Local and International agents. Why didn't we create a single interface with a discount property added?

Let's try that first
public interface IAgent
{
    string AgentName { get; }

    string Location { get; }

    double Discount { get; }
}

If we use the above interface for International agents, all the properties can be utilized in the method. But what if we use it for to define local agents? No issues as such, but the property Discount has no impact on Local Agent class. This violates the Interface Segregation Principle. How? We are forcing Local agent class to use Discount property.

Solution:
Remove Discount from IAgent interface. Solved.
public interface IAgent
{
    string AgentName { get; }

    string Location { get; }
}

public interface IInternationalAgent
{
    double Discount { get; }
}

Note: Please refer Part 3 for the detailed implementation.

Comments