Robert C Martin formalized ISP with his work at xerox. When a client depends upon an interface, the client should not be forced to implement functionality the client does not have. Interfaces should be small so that clients only have to implement what they need.

Interface Segregation Principle improves readability and maintainability by cleaning out unneeded implementations.

It also cleans up unit tests, since less information is required when setting up a class.

ISP Example

The following code is a violation to the Interface Segregation Principle due to the interface forcing the extra method which is not required by the class to be implemented. Since it throws a NotImplementedException it now also violates the Liskov Substitution Principle.


  interface IDinnerTime
  {
      void Eat();
      void SetTable();
  }

  public class Monster : IDinnerTime
  {
      public void Eat()
      {
          Console.WriteLine("Nom Nom Nom");
      }

      public void SetTable()
      {
          throw new NotImplementedException(); // Monsters dont set the table (LSP violation as well)
      }
  }
    
collapse
  • Break down interfaces into smaller cohesive interfaces so that they can be implemented separately.
  • You can make super interfaces which implement all the smaller interfaces.
  • If you don't own the fat interface to change it, implement the Adapter pattern.
  • If you don't own the clients to change it or the usage is too extensive, implement the Facade pattern

Check out these links for more info:

My design and architecture repo