Core Java

Solid Principles: Interface segregation principle

Previously we examined the liskov substitution principle. Next principle is the interface-segregation. The interface-segregation principle (ISP) states that no client should be forced to depend on methods it does not use.

Imagine an interface with many methods in our codebase and many of our classes implement this interface although only some of its methods are implemented.

In our case the Athlete interface is an interface with some actions of an athlete.

package com.gkatzioura.solid.segragation;

public interface Athlete {

    void compete();

    void swim();

    void highJump();

    void longJump();

}

We have added the method compete but also there some extra methods like swim highJump and longJump.
Suppose that JohnDoe is a swimming athlete. By implementing the Athlete interface we have to implement methods like highJump and longJump which JohnDoe will never use.

package com.gkatzioura.solid.segragation;

public class JohnDoe implements Athlete {

    @Override 
    public void compete() {
        System.out.println("John Doe started competing");
    }

    @Override 
    public void swim() {
        System.out.println("John Doe started swimming");
    }

    @Override 
    public void highJump() {
    }

    @Override 
    public void longJump() {
    }
}

The same problem will occur for another athlete who might be a field Athlete competing on high jump and long jump.
We will follow the interface segregation principle and we will refactor the original interface and create two other interfaces one for Jumping athletes and one for Swimming athletes.

package com.gkatzioura.solid.segragation;

public interface SwimmingAthlete extends Athlete {

    void swim();

}
package com.gkatzioura.solid.segragation;

public interface JumpingAthlete extends Athlete {

    void highJump();

    void longJump();

}

And therefore John Doe will not have to implement actions that he is not capable of performing.

package com.gkatzioura.solid.segragation;

public class JohnDoe implements SwimmingAthlete {

    @Override
    public void compete() {
        System.out.println("John Doe started competing");
    }

    @Override
    public void swim() {
        System.out.println("John Doe started swimming");
    }

}

You can find the source code on github. The last principle is the dependency inversion principle.

Also I have compiled a cheat sheet containing a summary of the solid principles.
Sign up in the link to receive it.

Published on Java Code Geeks with permission by Emmanouil Gkatziouras, partner at our JCG program. See the original article here: Solid Principles: Interface segregation principle

Opinions expressed by Java Code Geeks contributors are their own.

Emmanouil Gkatziouras

He is a versatile software engineer with experience in a wide variety of applications/services.He is enthusiastic about new projects, embracing new technologies, and getting to know people in the field of software.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button