SOLID: Interface Segregation Principle (ISP)

“No client should be forced to depend on methods it does not use.”

Real World Analogy: Suppose, you go to a market to buy a charger for your iPhone.And you purchased a charger which has iPhone plug but also has Samsung, Nokia and Moto plugs too, which you never gonna use. That’s what an ISP solves, making sure you get an iPhone charger only.

Sometimes when we create interfaces we see the

  • Empty implementations
  • Big Interface with too many methods
  • Methods are not relevant to each other (Low Cohesion)

This all indicates the violation of ISP.

To understand this lets see an example below,

At first, glance it looks okay but when we have the implementation of this it would have the issues.

Woaha ! so many empty methods, Meaning there is some issue at the abstraction layer.

Plus If download is called on AudioPlayer instance would not work same as the contract, breaking the LSV rule.

We can easily solve this by breaking or decomposing the MediaPlayable as below and make high cohesive small interfaces.

Fat protocols sound like “Too many responsibilities” for an interface (SRP)
& no empty implementations meaning more likely to break LSV violation.

Yes, all SOLID principles are intertwined with each other.

Lets move to our last rule Dependency Inversion Principle

iOS Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store