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

概述

定义:客户端不应该依赖它不需要的接口,一个类对另一个类的依赖应该建立在最小的接口上。简单地说,就是不要定义方法多而全的总接口,尽量细化接口,接口中的方法尽量少。

案例演示:

类C通过通过接口依赖(使用)类A,但是只会使用到1、2、3方法,类D通过接口依赖(使用)类B,但是只会使用到1、4、5方法

未使用接口隔离原则的设计

在这里插入图片描述
代码:

public interface TotalInterface {
    void operation1();
    void operation2();
    void operation3();
    void operation4();
    void operation5();
}
public class TotalInterfaceImplA implements TotalInterface {
    @Override
    public void operation1() {
        System.out.println("类A实现了接口的方法1");
    }

    @Override
    public void operation2() {
        System.out.println("类A实现了接口的方法2");
    }

    @Override
    public void operation3() {
        System.out.println("类A实现了接口的方法3");
    }

    @Override
    public void operation4() {
        System.out.println("类A实现了接口的方法4");
    }

    @Override
    public void operation5() {
        System.out.println("类A实现了接口的方法5");
    }
}
public class TotalInterfaceImplB implements TotalInterface {
    @Override
    public void operation1() {
        System.out.println("类B实现了接口的方法1");
    }

    @Override
    public void operation2() {
        System.out.println("类B实现了接口的方法2");
    }

    @Override
    public void operation3() {
        System.out.println("类B实现了接口的方法3");
    }

    @Override
    public void operation4() {
        System.out.println("类B实现了接口的方法4");
    }

    @Override
    public void operation5() {
        System.out.println("类B实现了接口的方法5");
    }
}
public class C {
    public void depend1(TotalInterface t) {
        t.operation1();
    }

    public void depend2(TotalInterface t) {
        t.operation2();
    }

    public void depend3(TotalInterface t) {
        t.operation3();
    }
}
public class D {
    public void depend1(TotalInterface t) {
        t.operation1();
    }

    public void depend4(TotalInterface t) {
        t.operation4();
    }

    public void depend5(TotalInterface t) {
        t.operation5();
    }
}
public class SegregationTest {
    public static void main(String[] args) {
        C c = new C();
        c.depend1(new TotalInterfaceImplA());
        c.depend2(new TotalInterfaceImplA());
        c.depend3(new TotalInterfaceImplA());

        D d = new D();
        d.depend1(new TotalInterfaceImplB());
        d.depend4(new TotalInterfaceImplB());
        d.depend5(new TotalInterfaceImplB());
    }
}

上述代码中的问题:类C通过通过接口依赖(使用)类A,但是只会使用到1、2、3方法,类D通过接口依赖(使用)类B,但是只会使用到1、4、5方法,如果接口TotalInterface对类C和类D来说不是最小接口,那么类A和类B必须实现他们不需要的方法。

使用接口隔离原则的设计

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

接口TotalInterface中出现的方法,根据实际情况拆分成三个接口:

在这里插入图片描述
代码:

public interface Interface1 {
    void operation1();
}
public interface Interface2 {
    void operation2();
    void operation3();
}
public interface Interface3 {
    void operation4();
    void operation5();
}
public class ImplA implements Interface1, Interface2 {
    @Override
    public void operation1() {
        System.out.println("类A实现了接口1的方法1");
    }

    @Override
    public void operation2() {
        System.out.println("类A实现了接口2的方法2");
    }

    @Override
    public void operation3() {
        System.out.println("类A实现了接口2的方法3");
    }
}
public class ImplB implements Interface1, Interface3 {
    @Override
    public void operation1() {
        System.out.println("类B实现了接口1的方法1");
    }

    @Override
    public void operation4() {
        System.out.println("类B实现了接口3的方法4");
    }

    @Override
    public void operation5() {
        System.out.println("类B实现了接口3的方法5");
    }
}
//类C通过接口Interface1、Interface2依赖(使用)类A
public class C {
    public void depend1(Interface1 i) {
        i.operation1();
    }

    public void depend2(Interface2 i) {
        i.operation2();
    }

    public void depend3(Interface2 i) {
        i.operation3();
    }
}
//类D通过接口Interface1、Interface3依赖(使用)类B
public class D {
    public void depend1(Interface1 i) {
        i.operation1();
    }

    public void depend4(Interface3 i) {
        i.operation4();
    }

    public void depend5(Interface3 i) {
        i.operation5();
    }
}
public class SegregationTest {
    public static void main(String[] args) {
        C c = new C();
        c.depend1(new ImplA());
        c.depend2(new ImplA());
        c.depend3(new ImplA());

        D d = new D();
        d.depend1(new ImplB());
        d.depend4(new ImplB());
        d.depend5(new ImplB());
    }
}

注意:

适度使用原则,如果接口设计的过小,里面的方法过少,则会造成接口的数量过多,使程序设计的复杂性增加。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值