SOLID principles by example: Interface segregation

This post continues the analisys of the SOLID principles and it’s about the Interface Segregration Principle (ISP).

Screenshot_1.png

Definition

The interface-segregation principle (ISP) states that no client should be forced to depend on methods it does not use.

The bad example

Here we examine an interface that violates ISP:

interface ISmartDevice
{
    void Print();

    void Fax();

    void Scan();
}
This interface states that a smart device is able to print, fax and scan. An implementation of this interface could be an AllInPrinter class:
class AllInOnePrinter : ISmartDevice
{
    public void Print()
    {
         // Printing code.
    }

    public void Fax()
    {
         // Beep booop biiiiip.
    }

    public void Scan()
    {
         // Scanning code.
    }
}

Simple, isn’t it? Right. Now suppose we need to handle a dumb device (EconomicPrinter class) that can only print. We’re forced to implement the Whole interface, for example:

class EconomicPrinter : ISmartDevice
{
    public void Print()
    {
        //Yes I can print.
    }

    public void Fax()
    {
        throw new NotSupportedException();
    }

    public void Scan()
    {
        throw new NotSupportedException();
    }
}

This is not very good.

The good example

Here we apply the ISP and we separate the single ISmartDevice interface into 3 smaller interfaces: IPrinter, IFax, and IScanner.

interface IPrinter{
    void Print();
}

interface IFax{
    void Fax();
}

interface IScanner{
    void Scan();
}

This way it’s easier to implement classes that do not need to handle all the original functionalities of the ISmartDevice interface like our EconomicPrinter. Our code is more decoupled and easier to mantain. Let’s re-implement our EconomicPrinter with this architecture:

class EconomicPrinter : IPrinter
{
    public void Print()
    {
         // Printing code.
    }

}

The original AllInOnePrinter now looks like this:

class AllInOnePrinter : IPrinter, IFax, IScanner
{
    public void Print()
    {
         // Printing code.
    }

    public void Fax()
    {
         // Beep booop biiiiip.
    }

    public void Scan()
    {
         // Scanning code.
    }

}

TL;DR

The ISP guides us to create many small interfaces with coherent functionalities instead of a few big interfaces with lots of different methods. When we apply the ISP, class and their dependencies communicate using focussed interfaces, minimising dependencies. Smaller interfaces are easier to implement, improving flexibility and the possibility of reuse.

One thought on “SOLID principles by example: Interface segregation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.