Interface Segregation Principle

Don't force pieces that don't fit

Intro

The above quote describes this principle in the shortest possible form, but how? 🤔  You will soon find out.

 

Previously, we discussed Liskov Substitution Principle and we came to know that having empty methods or returning not implemented exception is a violation of LSP. In addition, we also discussed the Single Responsibility Principle which in short says a class should only be responsible for a scoped functionality.  

 

You will be wondering now, why I am reminding you of previous topics?  🤔

 

Believe me when I tell you there is a 🪄  magical 🪄 connection between the three topics 🤯  but I am not going to spoil the fun and tell you right away, instead, we will find the magical connection together. Let’s start with the principle definition. 

Principle Definition

Clients should not be forced to depend on methods they do not use.

Robert C. Martin

To understand the definition in today’s linguistics change the word Clients to Developers and depend on to implement. Now our version of the definition says. 

Developers should not be forced to implement methods they do not use.

The definition is crystal clear, but it still does not convey the full picture yet. As we know the principle name is Interface Segregation Principle,  so where is the segregation part in the definition? 🤔 

 

well, the devil is in the details 👺 . But don’t worry, we will soon unveil the details.

Real-life Example

I am new born baby don't expect much from me
i will be able to do some stuff by myself.
it is time to get things seriosuly
it is time spread wisdom

Everything in life moves, nothing stops, and humans are no exception. We are all getting older day by day and we fall under a certain age group. 

 

Therefore, we can say that humans can be segregated by age group. For instance, newborn, kid, adult and elderly.  This segregation can be seen everywhere around us. For instance, clothing shops categorise clothes by age, medications are given according to age and symptoms, a lot of country rules are bounded around age group, just to mention a few.

 

The same should be achieved when writing a code, we should be able to segregate interfaces according to interface responsibility. I know you want a code example by now. No worries here it comes.

 

Case Scenario

Assume that your task is to write an interface that describes human actions. The interface will be used for a game (e.g. The Sims), a game that simulates humans’ real-life actions. Now Let’s think about this interface.

To begin with, humans can do a lot of actions and those actions are usually bounded to a certain age group. Therefore, it wouldn’t be a logical thing to write all the actions that humans can do in one fat interface, why? Let’s try it to find out.

Now let’s assume the following scenarios. 

1- implement a crying baby functionality.

2- implement adult getting married functionality.

 

In this case, you will need two classes one for the baby and another for the adult and both will implement a Human interface. Easy right? But hold on!!

 

You will leave empty or non implemented methods in both cases and  💣   BOOOM 💣💥  LSP violation

 

What we should do? let’s take a hint from the below meme.

As the meme suggested we should shrink the interface, what we mean by this is segregating the human interface into multiple scoped interfaces. Our scope for the interface will be age group as follows.

interface Human {
    fun celebrateBirthday(birthDate: Date)
    fun breath()
    fun die(deathDateAndTime: DateTime)
}
interface NewBorn {
    fun crawl(duration: Duration)
    fun cry(duration: Duration, reason: CryingReason)
    fun play(duration: Duration)
    fun wearDiaper(duration: Duration)
}
interface Kid {
    fun goToSchool()
}
interface Adult {
    fun goToCollege(collegeDetails: CollegeDetails)
    fun work(companyDetails: CompanyDetails)
    fun getMarried(venueDetails: VenueDetails)
}
interface Elderly {
    fun mentorYoungerGeneration()
    fun speakWisely()
}

As seen above we segregated the human interface into multiple scoped interfaces, but why? 

The principle emphasises the creation of interfaces with scoped purpose, as a result, we will gain the following benefits.

  • limiting the scope of change in the future.
  • avoiding LSP violation.
  • obeying the single responsibility principle
  • understanding the purpose of the interface will be much easier.

Let me know in the comments if you have any additional benefits.

Conclusion

This principle was easy to digest but yet have a great impact on the code. However, while this principle leans towards smaller interfaces, it does not mean taking it to the extreme and create multiple interfaces for no logical reason. You will have to reason out the situation and due diligence. 

Support Ideas

If you liked the content and thought of supporting the creator, below are some ideas to do so.

  • your engagement in the comment will help giving more value to the content.
  • spreading the content in social media will help attract more readers and google ranking
Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on email
You Can

Buy me a Coffee

  • also consider subscribing to the newsletter list

2 Comments

  1. You explained a tricky principle in simple and fun way..
    Thank you

Leave a Reply

Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124