Sobes.tech
Junior — Middle

Can you explain the principle of interface segregation from SOLID and its importance in software design?

sobes.tech AI

Answer from AI

The Interface Segregation Principle (ISP) from SOLID states that clients should not depend on interfaces they do not use. It is better to have several specialized interfaces than one large universal interface.

This is important because:

  • It allows creating more flexible and understandable interfaces.
  • It reduces coupling between components.
  • It facilitates maintenance and extension of the code.
  • It prevents the need to implement unnecessary methods.

Example in Java:

interface Printer {
    void print(Document d);
}

interface Scanner {
    void scan(Document d);
}

class MultiFunctionPrinter implements Printer, Scanner {
    public void print(Document d) { /* implementation */ }
    public void scan(Document d) { /* implementation */ }
}

class SimplePrinter implements Printer {
    public void print(Document d) { /* implementation */ }
}

If there was a single large interface with print and scan methods, SimplePrinter would have to implement the unnecessary scan method, which violates ISP.