Java设计模式之接口隔离原则精解

 更新时间:2022年02月08日 11:40:20   作者:张起灵-小哥  
设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。本篇介绍设计模式七大原则之一的接口隔离原则

1.什么是接口隔离原则?

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

2.对应代码

上面这张图呢,就违反了接口隔离原则。它对应的代码如下:👇👇👇

package com.szh.principle.segregation;
 
/**
 *
 */
interface Interface1 {
    void operation1();
    void operation2();
    void operation3();
    void operation4();
    void operation5();
}
 
class B implements Interface1 {
    public void operation1() {
        System.out.println("B 实现了 operation1");
    }
    public void operation2() {
        System.out.println("B 实现了 operation2");
    }
    public void operation3() {
        System.out.println("B 实现了 operation3");
    }
    public void operation4() {
        System.out.println("B 实现了 operation4");
    }
    public void operation5() {
        System.out.println("B 实现了 operation5");
    }
}
 
class D implements Interface1 {
    public void operation1() {
        System.out.println("D 实现了 operation1");
    }
    public void operation2() {
        System.out.println("D 实现了 operation2");
    }
    public void operation3() {
        System.out.println("D 实现了 operation3");
    }
    public void operation4() {
        System.out.println("D 实现了 operation4");
    }
    public void operation5() {
        System.out.println("D 实现了 operation5");
    }
}
 
class A { //A 类通过接口Interface1 依赖(使用) B类,但是只会用到1,2,3方法
    public void depend1(Interface1 i) {
        i.operation1();
    }
    public void depend2(Interface1 i) {
        i.operation2();
    }
    public void depend3(Interface1 i) {
        i.operation3();
    }
}
 
class C { //C 类通过接口Interface1 依赖(使用) D类,但是只会用到1,4,5方法
    public void depend1(Interface1 i) {
        i.operation1();
    }
    public void depend4(Interface1 i) {
        i.operation4();
    }
    public void depend5(Interface1 i) {
        i.operation5();
    }
}
 
public class Segregation1 {
    public static void main(String[] args) {
        A a = new A();
        a.depend1(new B()); // A类通过接口去依赖B类
        a.depend2(new B());
        a.depend3(new B());
 
        C c = new C();
        c.depend1(new D()); // C类通过接口去依赖(使用)D类
        c.depend4(new D());
        c.depend5(new D());
    }
}

代码虽然很长,但是不难理解。A类依赖了B类,但是只会用到顶级接口中的1、2、3这三个方法;而C类依赖了D类,但是只会用到顶级接口中的1、4、5这三个方法,也就是说在A和B这两个类的层面上而言,和顶级接口中的4、5两个方法是没什么关联的,那么B类在实现顶级接口的时候就没必要重写4、5这两个方法了。但是这里有一个问题就是顶级接口中包括了1到5这五个方法,你如果实现这个接口就必须重写这五个方法,那么我们就可以考虑将顶级接口拆分成多个接口,需要用到哪个就实现哪个,这也就是所谓的接口隔离了。

3.改进代码

经过上面的一番叙述,我们可以将代码改写成下面的形式。

即将顶级接口拆分成3个小接口,B、D两个类根据实际情况该实现哪个接口就实现哪个接口(因为这五个方法已经被分开了)。

package com.szh.principle.segregation.improve;
 
/**
 *
 */
interface Interface1 {
    void operation1();
}
 
interface Interface2 {
    void operation2();
    void operation3();
}
 
interface Interface3 {
    void operation4();
    void operation5();
}
 
class B implements Interface1, Interface2 {
    public void operation1() {
        System.out.println("B 实现了 operation1");
    }
 
    public void operation2() {
        System.out.println("B 实现了 operation2");
    }
 
    public void operation3() {
        System.out.println("B 实现了 operation3");
    }
}
 
class D implements Interface1, Interface3 {
    public void operation1() {
        System.out.println("D 实现了 operation1");
    }
 
    public void operation4() {
        System.out.println("D 实现了 operation4");
    }
 
    public void operation5() {
        System.out.println("D 实现了 operation5");
    }
}
 
class A { // A 类通过接口Interface1,Interface2 依赖(使用) B类,但是只会用到1,2,3方法
    public void depend1(Interface1 i) {
        i.operation1();
    }
 
    public void depend2(Interface2 i) {
        i.operation2();
    }
 
    public void depend3(Interface2 i) {
        i.operation3();
    }
}
 
class C { // C 类通过接口Interface1,Interface3 依赖(使用) D类,但是只会用到1,4,5方法
    public void depend1(Interface1 i) {
        i.operation1();
    }
 
    public void depend4(Interface3 i) {
        i.operation4();
    }
 
    public void depend5(Interface3 i) {
        i.operation5();
    }
}
 
public class Segregation2 {
    public static void main(String[] args) {
        A a = new A();
        a.depend1(new B()); // A类通过接口去依赖B类
        a.depend2(new B());
        a.depend3(new B());
 
        C c = new C();
        c.depend1(new D()); // C类通过接口去依赖(使用)D类
        c.depend4(new D());
        c.depend5(new D());
    }
}

4.接口隔离原则总结

  1. 类A通过接口Interface1依赖类B,类C通过接口Interfacel依赖类D,如果接口Interface1对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不需要的方法。
  2. 将接口Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则。

到此这篇关于Java设计模式之接口隔离原则精解的文章就介绍到这了,更多相关Java 接口隔离原则内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • PowerJob的ProcessorLoader工作流程源码解读

    PowerJob的ProcessorLoader工作流程源码解读

    这篇文章主要为大家介绍了PowerJob的ProcessorLoader工作流程源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • 浅析Java中线程的创建和启动

    浅析Java中线程的创建和启动

    这篇文章运用实例代码介绍了Java中线程的创建和启动,非常详细,有需要的朋友们可以参考借鉴,下面一起来看看。
    2016-08-08
  • Java抢红包的红包生成算法

    Java抢红包的红包生成算法

    现在日常生活中抢红包已经成了日常游戏,本篇文章主要介绍了Java抢红包的红包生成算法,具有一定的参考价值,有需要的可以了解一下。
    2016-11-11
  • Java多线程之Interrupt中断线程详解

    Java多线程之Interrupt中断线程详解

    Interrupt 的其作用是"中断"线程, 但实际上线程仍会继续运行, 这是一个非常容易混淆的概念. Interrupt 的真正作用是给线程对象设置一个中断标记, 并不会影响线程的正常运行,需要的朋友可以参考下
    2021-05-05
  • 简单了解Spring IoC相关概念原理

    简单了解Spring IoC相关概念原理

    这篇文章主要介绍了简单了解Spring IoC相关概念原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • Maven依赖冲突原因以及解决方法

    Maven依赖冲突原因以及解决方法

    依赖冲突是指项目依赖的某一个 jar 包,有多个不同的版本,因而造成类包版本冲突依赖冲突很经常是类包之间的间接依赖引起的,本文将给大家介绍Maven依赖冲突原因以及解决方法,需要的朋友可以参考下
    2023-12-12
  • Java定义画板类的方法

    Java定义画板类的方法

    这篇文章主要为大家详细介绍了Java定义画板类的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • Spring MVC注解式开发示例完整过程

    Spring MVC注解式开发示例完整过程

    这篇文章主要介绍了Spring MVC注解式开发示例完整过程,MVC注解式开发即处理器基于注解的类开发,对于每一个定义的处理器,无需在xml中注册,只需在代码中通过对类与方法的注解,即可完成注册
    2023-02-02
  • Linux中JDK安装配置教程

    Linux中JDK安装配置教程

    这篇文章主要为大家详细介绍了Linux中JDK安装配置教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • Windows10 Java环境变量配置过程图解

    Windows10 Java环境变量配置过程图解

    这篇文章主要介绍了Windows10 Java环境变量配置过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07

最新评论