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

接口隔离原则: 客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上。
违反原则的样例
类图如下:
在这里插入图片描述
这里A依赖B的123方法,C依赖D的145方法,如果按照类图的做法,BD都必须实现接口的所有方法,这就违反了接口隔离原则
代码如下:

public class SingleFantasy {
    public static void main(String[] args) {
        A a = new A();
        B b = new B();
        C c = new C();
        D d = new D();
        a.depend1(b);
        a.depend2(b);
        a.depend3(b);
        c.depend1(d);
        c.depend4(d);
        c.depend5(d);
    }
}

interface interface1{
    public void operation1();
    public void operation2();
    public void operation3();
    public void operation4();
    public void operation5();
}

class B implements interface1{

    @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方法");
    }
}

class D implements interface1{

    @Override
    public void operation1() {
        System.out.println("D 实现接口的1方法");
    }

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

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

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

    @Override
    public void operation5() {
        System.out.println("D 实现接口的5方法");
    }
}

class A{
    public void depend1(interface1 t){
        t.operation1();
    }
    public void depend2(interface1 t){
        t.operation2();
    }
    public void depend3(interface1 t){
        t.operation3();
    }
}

class C{
    public void depend1(interface1 t){
        t.operation1();
    }
    public void depend4(interface1 t){
        t.operation4();
    }
    public void depend5(interface1 t){
        t.operation5();
    }
}
//运行结果
B 实现接口的1方法
B 实现接口的2方法
B 实现接口的3方法
D 实现接口的1方法
D 实现接口的4方法
D 实现接口的5方法

这里BD都实现了12345方法,但是并没有把这些方法都用完,正确的写法应该是下面这样的。
在这里插入图片描述

把接口的粒度细分,方BD去继承不同的接口,实现指定的方法
代码如下:

public class InterfaceIsolation{
    public static void main(String[] args) {
        A a = new A();
        B b = new B();
        C c = new C();
        D d = new D();
        a.depend1(b);
        a.depend2(b);
        a.depend3(b);
        c.depend1(d);
        c.depend4(d);
        c.depend5(d);
    }
}

interface interface1{
    public void operation1();
}
interface interface2{
    public void operation2();
    public void operation3();
}
interface interface3{
    public void operation4();
    public void operation5();
}
class B implements interface1, interface2{

    @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方法");
    }


}

class D implements interface1, interface3{

    @Override
    public void operation1() {
        System.out.println("D 实现接口的1方法");
    }

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

    @Override
    public void operation5() {
        System.out.println("D 实现接口的5方法");
    }
}

class A{
    public void depend1(interface1 t){
        t.operation1();
    }
    public void depend2(interface2 t){
        t.operation2();
    }
    public void depend3(interface2 t){
        t.operation3();
    }
}

class C{
    public void depend1(interface1 t){
        t.operation1();
    }
    public void depend4(interface3 t){
        t.operation4();
    }
    public void depend5(interface3 t){
        t.operation5();
    }
}
//结果如下
B 实现接口的1方法
B 实现接口的2方法
B 实现接口的3方法
D 实现接口的1方法
D 实现接口的4方法
D 实现接口的5方法
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值