Solid principles: 4. Interface segregation principle

Today, this lesson will be rather short, because is about the fourth principle of SOLID, the principle of segregation of interfaces.

The principle of segregation of interfaces is very simple, says not to create interfaces with methods that the class does not use, the interfaces should be as small as possible.

So this rule can be created for this rule in the style:

Interfaces should be small, so that later the classes do not implement methods they do not need, the interfaces should be concret and as specific as possible.

It is better to use abstract classes to create base types, because they can describe a specific type, have appropriate attributes and methods, interfaces are stateless so they should not describe business logic. Interfaces should only inform the programmer about any behavior.

Look at the code below:

This interface is incorrect because not all methods from this interface match and used.

It’s better to do two separate interfaces like this:

As you can see by dividing the interfaces, we keep order. Thanks to this, derived classes do not have methods that they do not need.


That’s all about Interface segregation principle.

This content also you can find on my steemit blog

And on medium:

As a standard, I remind you about the newsletter, which I send notifications about new entries and additional information about the IT world in general.🙂

And NECESSERILY join the DevmanCommunity community on fb, part of the community is in one place 🙂

– site on fb:ławomir Kowalski

– group on fb: DevmanCommunity

Ask, comment underneath at the end of the post, share it, rate it, whatever you want🙂.

If that post was useful for you share it with your friends :)

Post a comment

Notify about