Introduce a,

It’s easy to write code where a class implements a large, bloated interface just to use some of the methods in that interface. This not only exposes the inner details of the class, but also increases the class’s complexity and contaminates the interface. To solve this problem, the ISP: Interface Segregation Principle was developed.

Second, the definition of

A client should not rely on interfaces it does not need. In other words, a class’s dependency on another class should be based on the smallest interface.

Three, use

Take, for example, the scenario where an employee goes to work

/** * Employee interface */
public interface IStaff {

    /** * Recruitment */
    void recruit(a);

    /** * write code */
    void code(a);

    /** * at work */
    void slackOff(a);

}

/** * programmer */
public class Coder implements IStaff {

    @Override
    public void recruit(a) {}@Override
    public void code(a) {}@Override
    public void slackOff(a) {}}/** * Hr */
public class Hr implements IStaff {

    @Override
    public void recruit(a) {}@Override
    public void code(a) {}@Override
    public void slackOff(a) {}}public class InterfaceSegregationPrinciple {

    public static void main(String[] args) {
        Hr hr = new Hr();
        hr.recruit();
        hr.slackOff();
        // Hr writes code?
        hr.code();

        Coder coder = new Coder();
        coder.code();
        coder.slackOff();
        // Is the programmer responsible for recruiting?coder.recruit(); }}Copy the code

From the above code, in addition to writing code and occasionally catching fish, programmers are also responsible for hiring? And Hr, in addition to hiring and the occasional fish, has to write code? This logic itself has certain problem, programmers and Hr are implemented an employee interface, this interface for programmers and Hr staff are not least interface, realized they don’t need to approach because it is over, then the employees interface split into separate three interfaces, respectively satisfy the programmers and the logic of Hr work work.

/** * Employee interface */
public interface IStaff {

    /** * at work */
    void slackOff(a);
}

/** * Programmer interface */
public interface ICoder {

    /** * write code */
    void code(a);
}

/** * Hr interface */
public interface IHr {

    /** * Recruitment */
    void recruit(a);
}

/** * programmer */
public class Coder implements ICoder.IStaff {

    @Override
    public void code(a) {}@Override
    public void slackOff(a) {}}/** * Hr */
public class Hr implements IHr.IStaff {

    @Override
    public void recruit(a) {}@Override
    public void slackOff(a) {}}public class InterfaceSegregationPrinciple {

    public static void main(String[] args) {
        Hr hr = new Hr();
        hr.recruit();
        hr.slackOff();

        Coder coder = newCoder(); coder.code(); coder.slackOff(); }}Copy the code

After interface split, programmers work only writing code, occasionally touch fish, and Hr work only responsible for hiring, occasionally touch fish, this satisfies the programmer and Hr work content this logic.

Four,

The interface isolation principle is greatly satisfied by breaking the interface into finer grained interfaces, where the caller can only access its own methods and cannot access methods that it should not, isolating the details of the implementation class.