设计模式七大原则 (2)——接口隔离原则


1、定义

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


2、应用实例

2.1不符合接口隔离原则

在这里插入图片描述

这个图的意思是:

  • 类A依赖接口Ⅰ中的方法1、2、3,类B是对类A的依赖实现;
  • 类C依赖接口Ⅰ中的方法1、4、5,类D是对类C的依赖实现;
  • 对于类B和D来说,由于它们实现接口Ⅰ,虽然它们存在用不到的方法,但是必须要去实现

代码

interface Interface {
    void ope1();
    void ope2();
    void ope3();
    void ope4();
    void ope5();
}

class B implements Interface {
    @Override
    public void ope1() {
        System.out.println("B 实现了 ope1");
    }

    @Override
    public void ope2() {
        System.out.println("B 实现了 ope2");
    }

    @Override
    public void ope3() {
        System.out.println("B 实现了 ope3");
    }

    // 由于实现接口,所以必须实现这两个方法
    @Override
    public void ope4() {}
    @Override
    public void ope5() {}
}

class D implements Interface {
    @Override
    public void ope1() {
        System.out.println("B 实现了 ope1");
    }

    // 由于实现接口,所以必须实现这两个方法
    @Override
    public void ope2() {}
    @Override
    public void ope3() {}

    @Override
    public void ope4() {
        System.out.println("B 实现了 ope4");
    }

    @Override
    public void ope5() {
        System.out.println("B 实现了 ope5");
    }
}

class A {
    public void depend1(Interface i) {
        i.ope1();
    }

    public void depend2(Interface i) {
        i.ope2();
    }

    public void depend3(Interface i) {
        i.ope3();
    }
}

class C {
    public void depend1(Interface i) {
        i.ope1();
    }

    public void depend4(Interface i) {
        i.ope4();
    }

    public void depend5(Interface i) {
        i.ope5();
    }
}

public class Client {
    public static void main(String[] args) {
        A a = new A();
        a.depend1(new B());
        a.depend2(new B());
        a.depend3(new B());

        C c = new C();
        c.depend1(new D());
        c.depend4(new D());
        c.depend5(new D());
    }
}

接口中出现的方法,不论对依赖它的类有没有用,实现类都必须去实现这些方法,这并不是一个好的设计。
所以,我们可以将接口Ⅰ进行拆分,根据情况,最好将接口Ⅰ拆分为三个接口。

2.2 符合接口隔离原则

在这里插入图片描述

我们将接口Ⅰ拆分为接口Ⅰ、接口Ⅱ和接口Ⅲ

  • 类A依赖接口Ⅰ中的方法1,接口Ⅱ中的方法2、3,类B是对类A的依赖实现;
  • 类C依赖接口Ⅰ中的方法1,接口Ⅲ中的方法4、5,类D是对类C的依赖实现。

代码

interface Interface0 {
    void ope1();
}

interface Interface1 {
    void ope2();
    void ope3();
}

interface Interface2 {
    void ope4();
    void ope5();
}

class B implements Interface0, Interface1 {
    @Override
    public void ope1() {
        System.out.println("B 实现了 ope1");
    }

    @Override
    public void ope2() {
        System.out.println("B 实现了 ope2");
    }

    @Override
    public void ope3() {
        System.out.println("B 实现了 ope3");
    }
}

class D implements Interface0, Interface2 {
    @Override
    public void ope1() {
        System.out.println("B 实现了 ope1");
    }

    @Override
    public void ope4() {
        System.out.println("B 实现了 ope4");
    }

    @Override
    public void ope5() {
        System.out.println("B 实现了 ope5");
    }
}

class A {
    public void depend1(Interface0 i) {
        i.ope1();
    }

    public void depend2(Interface1 i) {
        i.ope2();
    }

    public void depend3(Interface1 i) {
        i.ope3();
    }
}

class C {
    public void depend1(Interface0 i) {
        i.ope1();
    }

    public void depend4(Interface2 i) {
        i.ope4();
    }

    public void depend5(Interface2 i) {
        i.ope5();
    }
}

public class Test01 {
    public static void main(String[] args) {
        A a = new A();
        a.depend1(new B());
        a.depend2(new B());
        a.depend3(new B());

        C c = new C();
        c.depend1(new D());
        c.depend4(new D());
        c.depend5(new D());
    }
}

3、注意事项和细节

  • 接口尽量小,但是要适度。对接口进行细化可以提高程序的灵活性;但是过小会造成接口数量过多,使设计复杂化。
  • 为依赖接口的类定制服务,只暴露类需要的方法,不需要的方法隐藏起来。

参考信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值