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
Now every class that inherits from Shape should provide the implementation for these two methods. We have class Square which inherits from Shape.
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.
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
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
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