Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Solid Principles: Interface Segregation Principle

DZone's Guide to

Solid Principles: Interface Segregation Principle

This quick overview of the I in SOLID offers general advice for when and how best to implement the interface segregation principle.

· Java Zone ·
Free Resource

The CMS developers love. Open Source, API-first and Enterprise-grade. Try BloomReach CMS for free.

Previously, we examined the Liskov substitution principle. The next principle is 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 that 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 John Doe 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 in the high jump and long jump.

We will follow the interface segregation principle and refactor the original interface:

package com.gkatzioura.solid.segragation;

public interface Athlete {

    void compete();
}


Then we will 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 we will talk about is the dependency inversion principle.

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

Topics:
java ,solid principles ,interface segregation principle ,refactoring ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}