Interface Segregation Principle in Design Pattern

Interface Segregation Principle is a principle that you need to remember when defining interfaces so that they are as small as possible but still ensure to cover all the needs that we want. Avoid the fact that another class implements your interface but has no need to implement some of the methods of this interface in detail because, in the context of this class, these methods have no meaning.

For example, I have an interface that defines some activities of a human as follows:

If we implement this interface for newborn objects, we won’t need to implement run() and cook() methods because they can’t have these operations. In this case, either we will have to still add implementation for run() and cook() methods but no content:

or we will throw an exception when the user uses these methods:

This will confuse the user because even though they can call the run() and cook() methods on the Baby object, it’s nothing to them. We need to avoid it!

We can subdivide the Human interface into a number of other interfaces, for example:

and:

The interface is now only:

Humans, everyone must eat! 😀

Then, our Baby class only needs to implement the eat() method:

If you need to implement the run() and cook() methods for the student object, you just need to implement the Runner and Cooker interfaces for this student object:

So we have separated the Human interface into smaller, more meaningful interfaces. The code is also clearer, isn’t it?

Leave a Reply

Your email address will not be published. Required fields are marked *