Next in line in the solid principles is Interface Segregation principle.
The general definition of this principle is
Client should not be forced to implement interfaces they don’t use.
So, this means, any class should be using or implementing only the interface which it needs, for its functionality.
Lets take a real world example. In an organization, each department is having its responsibility, like Development department will work on projects, HR team will be responsible for hiring new members, business development will be responsible for getting new projects for the company and so on. So if we talk about the code which will handle this, it could be something like the following :
Now, as we look into the code, we need to see is our HR team need to be concerned about getting projects for company or our business development team need to do the development etc. ? All the concrete classes are required to implement the functions, which are of no use of them(even if its empty).
This is the violation of Interface Segregation Principle.
This type of interface is also called as fat or polluted interface. So to avoid this, we break the responsibilities of each department into their individual tasks and define their own interfaces. So our code will become like the following:
So after creating separate interfaces, each department is concerned about its own department, and does not what other departments are up-to. Now, if you even add any new functionality to any interfaces, only the concerned department will be required to implement it.
One important thing to notice here is that, if you closely observe this principle, it seems quite similar to the Single responsibility principle.
So this was all about the concept of Interface Segregation Principle.
And if you have any thoughts on this, then do share them.