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.

Summary

That’s all about Interface segregation principle.

This content also you can find on my steemit blog https://steemit.com/solid/@slawas/solid-principles-4-interface-segregation-principle

And on medium: https://medium.com/@sawomirkowalski/solid-principles-4-interface-segregation-principle-8c8c4106e64a

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: Devman.pl-Sł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

Be first!

avatar
  Subscribe  
Notify about