接口隔离原则

目录

1. 接口隔离原则

1.1 案例

1.1.1 改进方案:

1.2 总结


1. 接口隔离原则

  • 接口隔离原则(Interface Segregation Principle)

1.1 案例

  • 类图:

  •  代码
public class InterfaceSegregationPrincipleOne {
    public static void main(String[] args) {
        A  a = new A();
        B  b = new B();
        a.denpend1(b);
        a.denpend2(b);
        a.denpend3(b);
        C c =new C();
        D d = new D();
        c.denpend1(d);
        c.denpend4(d);
        c.denpend5(d);
    }
}
interface InterfaceA{
    void operation1();
    void operation2();
    void operation3();
    void operation4();
    void operation5();
}

class B implements InterfaceA{

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

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

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

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

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

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

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

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

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

    @Override
    public void operation5() {
        System.out.println("D实现operation5");
    }
}
class A {
    public void  denpend1(InterfaceA i){
        i.operation1();
    }
    public void  denpend2(InterfaceA i){
        i.operation2();
    }
    public void  denpend3(InterfaceA i){
        i.operation3();
    }
}

class C {
    public void  denpend1(InterfaceA i){
        i.operation1();
    }
    public void  denpend4(InterfaceA i){
        i.operation4();
    }
    public void  denpend5(InterfaceA i){
        i.operation5();
    }
}
  • 思考:存在的问题:类B、类D因为实现了接口InterfaceA,重写了接口中的所有方法;但是类B没有必要实现operation4、operation5方法(同理:类D中operation2、operation3也是没必要实现的)现象就是我们实现了接口中一些不必要的方法

1.1.1 改进方案:

  • 类图

  • 代码
public class InterfaceSegregationPrincipleTwo {
    public static void main(String[] args) {
        A1 aOne = new A1();
        B1 b1 =new B1();
        aOne.denpend1(b1);
        aOne.denpend2(b1);
        aOne.denpend3(b1);
        C1 c1 =new C1();
        D1 d1 =new D1();
        c1.denpend1(d1);
        c1.denpend4(d1);
        c1.denpend5(d1);

    }
}

interface InterfaceA1 {
    void operation1();
}

interface InterfaceA2 {
    void operation2();

    void operation3();
}

interface InterfaceA3 {
    void operation4();

    void operation5();
}

class B1 implements InterfaceA1, InterfaceA2 {

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

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

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

class D1 implements InterfaceA1, InterfaceA3 {

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


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

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

class A1 {
    public void denpend1(InterfaceA1 i) {
        i.operation1();
    }

    public void denpend2(InterfaceA2 i) {
        i.operation2();
    }

    public void denpend3(InterfaceA2 i) {
        i.operation3();
    }
}

class C1 {
    public void denpend1(InterfaceA1 i) {
        i.operation1();
    }

    public void denpend4(InterfaceA3 i) {
        i.operation4();
    }

    public void denpend5(InterfaceA3 i) {
        i.operation5();
    }
}
  • 总结:

   核心思想:拆分接口,隔离接口

 

1.2 总结

接口隔离原则:

  • 客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖(如案例,类A通过接口依赖类B、类C通过接口依赖类D)应该建立在最小的接口上
  • 保证最小接口(如果不是最小接口,接口对应的实现类就要实现不必要的方法,如类图一中,类B多余实现了operation4、operation5类D多余实现了operation2、operation3)
  • 拆分接口(如改进后的接口InterfaceA1 、InterfaceA2、InterfaceA3)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值