Interface Segregation Principle (ISP)

Interface Segregation Principle (ISP)

In the previous article about SOLID, we talking about the Liskov Substitution Principle today we will talk about the Interface Segregation principles

What is the Interface Segregation principle?

“Clients should not be forced to depend upon interfaces that they do not use.”

Before we talking about our new solid principle we should know what is Interface, An interface is a description of the actions that an object can do... for example when you flip a light switch, the light goes on, you don't care how just that it does. In Object-Oriented Programming, an Interface is a description of all functions that an object must have in order to be an "X". Again, as an example, anything that "ACTS LIKE" a light, should have a turn_on() method and a turn_off() method. The purpose of interfaces is to allow the computer to enforce these properties and to know that an object of TYPE T (whatever the interface is ) must have functions called X, Y, Z, etc.

The interface segregation principle (ISP) is about creating very specialized interfaces for the client. It’s a good practice to create interfaces that are understandable from the perspective of the client. The Interface segregation principle encourages us to create different interfaces for different clients. The clients mentioned above are nothing but the classes implementing them.

Let’s take an example of an application that calculates the area and volume of a given shape. Following is a bad way to implement it.

Before implementing ISP we will make an interface called Shape as shown below

No alt text provided for this image

Now every class that inherits from Shape should provide the implementation for these two methods. We have class Square which inherits from Shape.

No alt text provided for this image

Now as calculate volume does not apply to the square we are returning zero. This is a bad design choice as the clients are not going to need all of the methods. The better solution will be to create two different interfaces for two-dimensional objects and three-dimensional objects.

No alt text provided for this image
No alt text provided for this image

Now the Square will implement TwoDimensionalShape and Cube will implement ThreeDimensionalShape. In this way, we can decouple code and make it more understandable.

Let's take another example from android consider you have two adapters for recycler view one of them only have click on the whole item and the second item have two buttons one for call and another one for the message sow you will build an interface without implementing ISP as shown below

No alt text provided for this image

Now if you implement the same interface for your adapters you will implement methods dose not implemented in your first adapter like on message clicked and on Call Clicked, for the second adapter it will work well so now let's refactor our code by making two interfaces with the only method every adapter of them need as shown below, this will apply ISP

No alt text provided for this image

Now you will implement the first interface for your first adapter inorder to handle the whole item click and the second one for the second adapter to handle call and message clicks ...

Any feedback is welcome 

Best regards: Anwar Samir

To view or add a comment, sign in

More articles by Anwar Samir

  • The Liskov Substitution Principle (LSP)

    In the previous article about SOLID, we talking about the Open close principle today we will talk about the Liskov…

  • Firebase Security Rules

    If you are using firebase in your android project you may receive an email telling you, You are not safe, you will be…

  • SLOID - Open Closed Principle(OCP)

    In the previous article, we talk about the first SOLID principles Single Responsibility Principle. In this tutorial, we…

  • SLOID - The Single Responsibility Principle (SRP)

    Before we start Before we start explaining we will explain every one of SOLID as a separate article in order to clarify…

    3 Comments
  • Service VS IntentService in Android

    Service is a base class of service implementation. Service class is run in the application’s main thread which may…

    18 Comments
  • Difference Between API and Web Service

    API vs Web Service API and Web service serve as a means of communication. The only difference is that a Web service…

  • Build your own android app and web service

    السلام عليكم كونك حد بيبرمج تطبيقات اندرويد فانت لازم تكون عارف يعني ايه ويب سيرفس وليه بتستعملها الفديو الجاي ده…

    1 Comment
  • ShareExperience

    لان الخبرة بتفرق شيراكسبرينس هو اول تطبيق مهتم يجمع الخبرات المختلفه من الناس اللي عندها الخبرات دي عشان انت تبدا تتعلم…

Others also viewed

Explore content categories