I In SOLID - Interface Segregation Principle (ISP)

In this article, you will learn about I in SOLID - Interface Segregation Principle (ISP).

Before discussing the ISP, we need to know about Interface.
 

What is Interface and what is the use of interface

 
As per Wikipedia “An INTERFACE in C# is a type definition like a class, except that it purely represents a contract between an object and its user. It can neither be directly instantiated as an object, nor can data members be defined. So, an interface is nothing but a collection of method and property declarations.”
 
So when an interface is the collection of method and property Declaration then where is the implementation?
 
Implementation will be in the class and interface need to be inherited to that class. So, it will be a contract between interface and class.
 
With the help of an interface, we can achieve loose coupling. The loose coupled architecture of the application helps to accommodate changes easily.
 
Please refer below CODE,
  1. interface IVehicle  
  2. {  
  3.     int NumberOfPeople { getset; }  
  4.     string Color { getset; }  
  5.     int SittingCapacity();   
  6.     bool Airbags { getset; }  
  7. }  
  8.   
  9. class TwoWheeler : IVehicle  
  10. {  
  11.     private int NumberOfPeople { getset; }  
  12.     public string Color { getset; }  
  13.     public bool Airbags { getset; }  
  14.               
  15.     public int SittingCapacity()  
  16.     {  
  17.         NumberOfPeople = 2;          
  18.         return NumberOfPeople;  
  19.     }      
  20. }  
  21.   
  22. class FourWheeler : IVehicle  
  23. {  
  24.     private int NumberOfPeople { getset; }  
  25.     public string Color { getset; }  
  26.     public bool Airbags { getset; }  
  27.               
  28.     public int SittingCapacity()  
  29.     {  
  30.         NumberOfPeople = 5;          
  31.         return NumberOfPeople;  
  32.     }      
  33. }  
This way we can inherit interface IVehicle interface to the class FourWheeler, FiveWheeler and so on.
 
Now, what is the role of ISP. “According to Wiki, ISP states that no client should be forced to depend on methods it does not use”. It means if one client needs 2 methods and 3 properties to use then there must be only 2 methods and 3 properties in the interface not more than that because the client is not using it. As we all know all properties and methods in the interface must be implemented in the inherited class, so it will be overhead of methods or properties for the client which the client is not using.
 
In this case, we need to separate the interfaces. In the above example of interface there is 1 property “Airbags” which is not getting used in TwoWheeler case, however, as it is declared in interface all the classes who are inheriting the interface IVehicle must implement the property. In this case it is the violation of Interface Segregation Principle (ISP). So to achieve ISP please refer below code.
  1. interface ITwoWheeler  
  2. {  
  3.     int NumberOfPeople { getset; }  
  4.     string Color { getset; }  
  5.     int SittingCapacity();   
  6.       
  7. }  
  8.   
  9. interface IFourWheeler  
  10. {  
  11.     int NumberOfPeople { getset; }  
  12.     string Color { getset; }  
  13.     int SittingCapacity();   
  14.     bool Airbags { getset; }  
  15. }  
And now we can implement the respective classes,
  1. class TwoWheeler : ITwoWheeler  
  2. {  
  3.     private int NumberOfPeople { getset; }  
  4.     public string Color { getset; }  
  5.       
  6.     public int SittingCapacity()  
  7.     {  
  8.         NumberOfPeople = 2;          
  9.         return NumberOfPeople;  
  10.     }      
  11. }  
  12.   
  13. class FourWheeler : IFourWheeler  
  14. {  
  15.     private int NumberOfPeople { getset; }  
  16.     public string Color { getset; }  
  17.     public bool Airbags { getset; }  
  18.               
  19.     public int SittingCapacity()  
  20.     {  
  21.         NumberOfPeople = 5;          
  22.         return NumberOfPeople;  
  23.     }      
  24. }  
ISP gives us to modularize and maintainable code so that everyone has its own class and interface, no one will interfere in others functionality.
 
--Happy Coding--