Share via


Interface Segregation Principle

If you are following the Software Design Principles while developing an application, the first thing that comes to your mind is the Interface Segregation Principle.  The idea for this principle is to use customer centric interface. When a customer wants some set of operations, we need to provide only those with the help of interface.  There should be a number of interfaces each related on an operation.

"when a client depends upon a class that contains interfaces that the client does not use, but other clients do use, then that client will be affected by the changes that those other clients force upon the class"

If we take an example,

public class  Rectangle
{
   public int  Width {get;set;}
   public int  Height {get;set;}
}

Here the Rectangle class has two properties. If the client uses the Rectangle class directly, if there is another class which says Square, then those adjustments will not be available to that customer. 

Rather there is a requirement of an interface, which the client use. 

public interface  IShape
{
   public int  Width {get;set;}
   public int  Height {get;set;}
}
 
public class  Rectangle : IShape
{
   public int  Width {get;set;}
   public int  Height {get;set;}
}
public class  Square : IShape
{
   public int  Width {get;set;}
   public int  Height {get;set;}
}

Thus if the client uses IShape instead of Rectangle, they will automatically get the functionality defined in Square or any other class that derives the IShape. 

See Also