Software

SOLID (I) – Interface Segregation Principle

The Interface Segregation principle states that a class should not be forced to depend on methods it does not use. This means that any classes that implement an interface, should not have “dummy” implementations of any methods defined in the interface. Instead, you should split large interfaces into smaller generalizations.

The example before applying the Interface Segregation principle:

Not Applied Interface Segregation Principle Class Diagram.png
Not applied Interface Segregation principle

The best way is to apply Interface Segregation principle applying splitting the ICashier interface into 2 smaller. This will allow each interface to be more accurate with its description of expected behaviors, and it will allow you to pick and choose which correct combinations of interfaces a concrete class should implement.

Improved design:

Applied Interface Segregation Principle Class Diagram.png
Applied Interface Segregation Principle

This way both of the concrete classes will only need to provide implementations for the interfaces that generalize their specific functionality.

The principle states that:

  • A class should not be forced to depend on methods it does not use.
  • Interfaces should be split up in such a way that it can properly describe the separate functionalities of the system.

Leave a comment

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