Before going through this article, I strongly recommend reading my previous articles:
The Interface Segregation Principle is one of the SOLID principles defined by Robert C. Martin. It is one of the rules of software development that says to always code according to a contract, in other words an interface, not against the implementation, in other words a concrete class, because coding against an interface provides advantages like flexibility, loose coupling, testable code and so on. This principle is related to creating an interface for implementation.
The principle says “
Client (class implementation interface) should not force to implement Interface that they don't use.” In simple words the principle is saying, do not design a big fat interface that forces the client to implement a method that is not required by it, instead design a small interface. So by doing this class only implement the required set of interface(s).
If there is big fat interface then break it into a set of small interfaces with the related method(s) in it. It's similar to normalizing our database like normalizing database from 1NF to 3NF where a big table is broken into tables with related columns.
Interface Segregation Principle in Real life
In terms of the violation of the ISP, the following image shows a big dustbin for throwing all kinds of garbage away without any kind of segregation.
Figure 1: Dustbin
With ISP, the following image is a good example of segregation in our real life.
Figure 2: ISP in Real Life
That is an image of a waste bin segregation that shows which one to use for throwing away trash we use.
Example of ISP in Application Development
Here is an example of a banking customer for a bank with the following types of customers:
The developer of a system defines an interface for a customer as in the following that doesn't follow the ISP rule.
Figure 3: Interface for customer
It looks OK at first glance but it's a big fat interface for the customer with problem since it forces the client class to implement methods that are not required.
A solution to the preceding problem is to split the fat interface into meaningfull parts, in other words small interfaces, so the customer type only implements the interface that is required by it.
The following is an image that follows the ISP:
Figure 4: Interface
Disadvantages