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.

1 commento su “SOLID principles by example: Interface segregation”

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.