Interface Segregation Principle (ISP) | SOLID PRINCIPLES

Interface Segregation Principle (ISP)

The Interface Segregation Principle (ISP) states that a class should not be forced to implement interfaces it does not use. In other words, it suggests that a class should not be burdened with methods it does not need. 

Example:


**Violation of ISP:**


Consider an interface that includes methods for both printing and scanning:



public interface IMachine

{

    void Print();

    void Scan();

}


Now, if you have a class that only needs to print but not scan, it would be forced to implement the `Scan` method, violating ISP:



public class SimplePrinter : IMachine

{

    public void Print()

    {

        // Implementation for printing

    }


    public void Scan()

    {

        // This method is not needed for SimplePrinter

        throw new NotImplementedException("SimplePrinter does not support scanning.");

    }

}



This violates ISP because `SimplePrinter` is forced to implement a method it doesn't need.


**Adhering to ISP:**


Refactor the code by creating separate interfaces for printing and scanning:



public interface IPrinter

{

    void Print();

}


public interface IScanner

{

    void Scan();

}

```


Now, classes can implement only the interfaces they need:


public class SimplePrinter : IPrinter

{

    public void Print()

    {

        // Implementation for printing

    }

}


This adheres to ISP, as each class now implements only the interfaces that are relevant to its functionality. It makes the system more modular and prevents classes from being burdened with unnecessary methods.

Real-world example related to a system that manages electronic devices.

Violation of ISP:

Suppose you have an `MultifunctionalPrinter` interface that includes methods for both printing and scanning:


public interface IMultifunctionalPrinter
{
    void Print();
    void Scan();
}


Now, if you have a class representing a basic printer that only needs to print, it would be forced to implement the `Scan` method:


public class BasicPrinter : IMultifunctionalPrinter
{
    public void Print()
    {
        // Implementation for printing
    }

    public void Scan()
    {
        // This method is not needed for BasicPrinter
        throw new NotImplementedException("BasicPrinter does not support scanning.");
    }
}


This violates ISP because `BasicPrinter` is forced to implement a method it doesn't need.

Adhering to ISP:

Refactor the code by creating separate interfaces for printing and scanning:


public interface IPrinter
{
    void Print();
}

public interface IScanner
{
    void Scan();
}


Now, classes can implement only the interfaces they need:


public class BasicPrinter : IPrinter
{
    public void Print()
    {
        // Implementation for printing
    }
}


This adheres to ISP, as each class now implements only the interfaces that are relevant to its functionality. It makes the system more modular, preventing classes from being burdened with unnecessary methods. If a class needs both printing and scanning capabilities, it can implement both interfaces.




Comments