七大原则二--->接口隔离原则

接口隔离原则

客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上

场景    

 

 接口Interface1  有5个方法

B  实现接口Interface1 的所有方法

D  实现接口Interface1 的所有方法

 

需求

类A 通过接口 Interface1 依赖 (使用) B类  只会用到方法1,2,3

类C 通过接口 Interface1 依赖 (使用) D类  只会用到方法1,4,5

 

问题 

如下代码违背了接口隔离原则    B类 4,5方法 类A 不需要用到      D类  2,3方法 类C不需要用到

interface Interface1 {
    void operation1();
    void operation2();
    void operation3();
    void operation4();
    void operation5();
}
/***
 *   B 实现接口Interface1 的所有方法
 * @author wf.zhang
 *
 */
class B implements Interface1 {
    public void operation1() {
        System.out.println("B 实现  operation1");
    }
    public void operation2() {
        System.out.println("B 实现  operation2");
    }
    public void operation3() {
        System.out.println("B 实现  operation3");
    }
    public void operation4() {
        System.out.println("B 实现  operation4");
    }
    public void operation5() {
        System.out.println("B 实现  operation5");
    }
}
/**
 * D  实现接口Interface1 的所有方法 
 * @author wf.zhang
 *
 */
class D implements Interface1 {
    public void operation1() {
        System.out.println("D  实现  operation1");
    }
    public void operation2() {
        System.out.println("D  实现  operation2");
    }
    public void operation3() {
        System.out.println("D  实现  operation3");
    }
    public void operation4() {
        System.out.println("D  实现  operation4");
    }
    public void operation5() {
        System.out.println("D  实现  operation5");
    }
}

/**
 *  A 通过接口 Interface1 依赖 (使用) B类  只会用到方法1,2,3
 * @author wf.zhang
 *
 */

class A {
    public void depend1(Interface1 i) {
        i.operation1();
    }
    public void depend2(Interface1 i) {
        i.operation2();
    }
    public void depend3(Interface1 i) {
        i.operation3();
    }
}
/**
 * C 通过接口 Interface1 依赖 (使用) D类  只会用到方法1,4,5
 * @author wf.zhang
 *
 */
class C {
    public void depend1(Interface1 i) {
        i.operation1();
    }
    public void depend4(Interface1 i) {
        i.operation4();
    }
    public void depend5(Interface1 i) {
        i.operation5();
    }
}
View Code

代码的关系图

 类A通过接口Interface1依赖类B,类C通过接口Interface1依赖类D,如果接口 Interface1对于类A和类C来说不是最小接口,

那么类B和类D必须去实现他们不需要的方法。

解决

接口Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立
依赖关系。也就是采用接口隔离原则

 

 

代码实现  关键在于  接口拆分达到最小接口

package com.wf.zhang.segregation.improve;

public class segregation1 {

    public static void main(String[] args) {

        A a =new A();
        //类A 通过接口 依赖类B
        a.depend1(new B()); 
        a.depend2(new B()); 
        a.depend3(new B());
        
        C c =new C();
        //类C 通过接口 依赖类D
        c.depend1(new D());
        c.depend4(new D());
        c.depend5(new D());
    }
}

interface Interface1 {
    void operation1();
}
interface Interface2 {
    void operation2();
    void operation3();
}
interface Interface3 {
    void operation4();
    void operation5();
}
/***
 *   B 实现接口Interface1 ,Interface2的所有方法
 * @author wf.zhang
 *
 */
class B implements Interface1,Interface2 {
    public void operation1() {
        System.out.println("B 实现  operation1");
    }
    public void operation2() {
        System.out.println("B 实现  operation2");
    }
    public void operation3() {
        System.out.println("B 实现  operation3");
    }
}
/**
 * D  实现接口Interface1,Interface3 的所有方法 
 * @author wf.zhang
 *
 */
class D implements Interface1,Interface3 {
    public void operation1() {
        System.out.println("D  实现  operation1");
    }
    public void operation4() {
        System.out.println("D  实现  operation4");
    }
    public void operation5() {
        System.out.println("D  实现  operation5");
    }
}

/**
 *  A 通过接口 Interface1,Interface2 依赖 (使用) B类  只会用到方法1,2,3
 * @author wf.zhang
 *
 */

class A {
    public void depend1(Interface1 i) {
        i.operation1();
    }
    public void depend2(Interface2 i) {
        i.operation2();
    }
    public void depend3(Interface2 i) {
        i.operation3();
    }
}
/**
 * C 通过接口 Interface1,Interface3 依赖 (使用) D类  只会用到方法1,4,5
 * @author wf.zhang
 *
 */
class C {
    public void depend1(Interface1 i) {
        i.operation1();
    }
    public void depend4(Interface3 i) {
        i.operation4();
    }
    public void depend5(Interface3 i) {
        i.operation5();
    }
}

 

posted @ 2020-02-13 17:05  wf.zhang  阅读(214)  评论(0编辑  收藏  举报