Interface Segregation Principle

Definition:

Many client specific interfaces are better than one general purpose interface. ISP states that clients should not be forced to implement interfaces they don't use.

Description:

Classes should not be forced to implement the interface members, that class don't need it. NotImplementedException with interface members is not a good design. The better design would be creating small specific interface with relevant methods instead of one common interface with more methods which we dont need it, then implement those interface member with NotImplementedException.

Example for ISP Vialoation:

Srp Vialoation

ITransfer - interface has Export and Import methods for export and import operations.

Meal - class implementes ITransfer. Export operation is allowed in Meal class. However, Import opeartion is not allowed in Meal class due to defined business rules. so Import() method does not have any implementation and expected to throw NotImplementedException.

WeeklyPlanner - class implementes ITransfer. Export and Import operations are allowed in WeeklyPlanner class.

interface ITransfer
{
    void Export(string toExport);
    void Import();
}

public class Meal : ITransfer
{
    public void Export(string toExport)
    {
        Console.WriteLine("Exported " + toExport);
    }

    public void Import()
    {
        //Meal information like Pancake, Pizza and etc needs to be added manually.
        throw new NotImplementedException();
    }
}

public class WeeklyPlanner : ITransfer
{

    public void Export(string toExport)
    {
        Console.WriteLine("Exported " + toExport);
    }

    public void Import()
    {
        //Weekly plans can be imported from previous weeks plan or added manually.
        Console.WriteLine("Imported weekly plan");
    }
}

class IspViolation
{
    private static void Main(string[] args)        
    {
        ITransfer planner = new WeeklyPlanner();
        planner.Export("download weekly plan");
        planner.Import();

        planner = new Meal();
        planner.Export("download available meal information");
        planner.Import();

        Console.ReadLine();
    }
}

Where is the violation ?

The above program is expected to throw the NotImplementedException in Import method from Meal class. since Import for Meal is not supported based on defined business rule. The common interface is used Meal & WeeklyPlanner classes. This current implementation violates ISP.

Fix for ISP Vialoation:

Srp Vialoation

IExport : interface has Export methods for export operation.

IImport : interface has Import methods for import operation.

Meal : Class implements IExport interface for supporting Export operation. since Import operation is not needed based on defined business rule.

WeeklyPlanner : Class implements IExport interface for supporting Export operation and implements IImport interface for supporting Import operation.

internal interface IExport
{
    void Export(string toExport);
}

internal interface IImport
{
    void Import();
}

public class Meal : IExport
{
    public void Export(string toExport)
    {
        Console.WriteLine("Exported " + toExport);
    }
}

public class WeeklyPlanner : IExport, IImport
{
    public void Export(string toExport)
    {
        Console.WriteLine("Exported " + toExport);
    }

    public void Import()
    {
        Console.WriteLine("Weekly plan is imported ");
    }
}

class Isp
{
    private static void Main(string[] args)        
    {
        var meal = new Meal();
        meal.Export("the meal");

        var planner = new WeeklyPlanner();
        planner.Export("the weekly plan");
        planner.Import();

        Console.ReadLine();
    }
}

ITransfer interface operation is replaced by IExport and IImport interfaces. The IExport and IImport interfaces having specific operation. The Meal class implements IExport interface and WeeklyPlanner class implements IExport and IImport interfaces. The generic ITransfer interface is segrecated with IExport and IImport interfaces for doing specific client operations.