设计模式七大原则之接口隔离

什么是接口隔离原则?

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

为什么要设计接口隔离

(1) 降低了耦合度
(2) 可以达到代码"瘦身效果"

案例演示 - 非接口隔离原则

可以先看下方的实现图

在这里插入图片描述
A类通过接口Interface01依赖于B类。C类通过接口Interface01依赖于D类。如上图所示Interface01接口对于A类和C类都不是最小接口,那么B类和D类也必须要去实现它们不需要的方法, 这样就造成了代码耦合度很高而且很冗余。

public class Normal {
    public static void main(String[] args) {
        A a = new A();
        a.operation01(new B());
        a.operation02(new B());
        a.operation03(new B());
        
        C c = new C();
        c.operation01(new D());
        c.operation04(new D());
        c.operation05(new D());
    }
}

interface Interface01 {
    void operation01();
    void operation02();
    void operation03();
    void operation04();
    void operation05();
}

/***
 * A类会使用到B的依赖,但是只会调用operation01()、operation02()、operation03()
 */
class B implements Interface01{

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

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

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

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

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

/***
 * C类会使用到D类的依赖,但是只会调用到operation01()、operation04()、operation05()
 */
class D implements Interface01{
    @Override
    public void operation01() {
        System.out.println("D 实现了 operation01()");
    }

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

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

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

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

class A {
    public void operation01(Interface01 interface01) {
        interface01.operation01();
    }

    public void operation02(Interface01 interface01) {
        interface01.operation02();
    }

    public void operation03(Interface01 interface01) {
        interface01.operation03();
    }
}

class C {
    public void operation01(Interface01 interface01) {
        interface01.operation01();
    }

    public void operation04(Interface01 interface01) {
        interface01.operation04();
    }

    public void operation05(Interface01 interface01) {
        interface01.operation05();
    }
}

案例演示 - 接口隔离原则

可以先看下方实现图

在这里插入图片描述
可以将接口Interface01拆分为独立的几个接口,这里可以拆分为三个独立的接口。A类和C类分别于它们需要的接口建立依赖关系。也就是采用接口隔离。

public class InterfaceQuarantine {
    public static void main(String[] args) {
        A a = new A();
        a.operation01(new B());
        a.operation02(new B());
        a.operation03(new B());

        C c = new C();
        c.operation01(new D());
        c.operation04(new D());
        c.operation05(new D());
    }
}

interface Interface01 {
    void operation01();
}

interface Interface02 {
    void operation02();
    void operation03();
}

interface Interface03 {
    void operation04();
    void operation05();
}

class B implements Interface01, Interface02 {

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

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

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

class D implements Interface01, Interface03 {

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

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

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

class A {
    public void operation01(Interface01 interface01) {
        interface01.operation01();
    }

    public void operation02(Interface02 interface02) {
        interface02.operation02();
    }

    public void operation03(Interface02 interface02) {
        interface02.operation03();
    }
}

class C {
    public void operation01(Interface01 interface01) {
        interface01.operation01();
    }

    public void operation04(Interface03 interface03) {
        interface03.operation04();
    }

    public void operation05(Interface03 interface03) {
        interface03.operation05();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值