设计模式-接口隔离原则

基本介绍

  1. 客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上
  2. 先看一张图:

  1. 类A通过接口Interface1 依赖类B,类C通过接口Interface1 依赖类D,如果接口Interface1对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不需要的方法。
  2. 按隔离原则应当这样处理:

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

传统代码

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/4/9 10:47
 */
public class Segregation1 {

    public static void main(String[] args) {

    }

}

interface Interface1 {
    void operation1();

    void operation2();

    void operation3();

    void operation4();

    void operation5();
}
class B implements Interface1{

    @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 Interface1{

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


// A类通过接口Interface 依赖(使用)B类,但是只会用到1,2,3方法
class A {
    public void depend1(Interface1 i){
        i.operation1();
    }
    public void depend2(Interface1 i){
        i.operation2();
    }
    public void depend3(Interface1 i){
        i.operation3();
    }
}

// C类通过接口Interface 依赖(使用)D类,但是只会用到1,4,5方法
class C{
    public void depend1(Interface1 i){
        i.operation1();
    }
    public void depend4(Interface1 i){
        i.operation4();
    }
    public void depend5(Interface1 i){
        i.operation5();
    }
}

应传统方法的问题和使用接口隔离原则改进

类A通过接口Interface1依赖类B,类C通过接口Interface1 依赖类D,如果接口Interface1 对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不需要的方法

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

接口Interface1中出现的方法,根据实际情况拆分为三个接口

代码示例:

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/4/9 10:47
 */
public class Segregation1 {

    public static void main(String[] args) {
        
    }
    
}

interface Interface1 {
    void operation1();

    void operation2();

    void operation3();

    void operation4();

    void operation5();
}
class B implements Interface1{

    @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 Interface1{

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


// A类通过接口Interface 依赖(使用)B类,但是只会用到1,2,3方法
class A {
    public void depend1(Interface1 i){
        i.operation1();
    }
    public void depend2(Interface1 i){
        i.operation2();
    }
    public void depend3(Interface1 i){
        i.operation3();
    }
}

// C类通过接口Interface 依赖(使用)D类,但是只会用到1,4,5方法
class C{
    public void depend1(Interface1 i){
        i.operation1();
    }
    public void depend4(Interface1 i){
        i.operation4();
    }
    public void depend5(Interface1 i){
        i.operation5();
    }
}

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-04-10 18:54:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-10 18:54:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-10 18:54:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-10 18:54:02       20 阅读

热门阅读

  1. PHP自带的密码加密函数Password_verify 和password_hash

    2024-04-10 18:54:02       14 阅读
  2. 第四百四十一回 再谈flutter_native_splash包

    2024-04-10 18:54:02       15 阅读
  3. Python基础语法

    2024-04-10 18:54:02       15 阅读
  4. 在API接口对接中关键示例问题(1)

    2024-04-10 18:54:02       14 阅读
  5. [leetcode]longest-consecutive-sequence 最长连续序列

    2024-04-10 18:54:02       12 阅读
  6. 免费泛域名SSL证书申请

    2024-04-10 18:54:02       12 阅读
  7. React 中与生命周期相关的 Hooks 及其使用示例

    2024-04-10 18:54:02       9 阅读
  8. React 组件生命周期函数的用法和示例代码

    2024-04-10 18:54:02       15 阅读
  9. jquery 正则自整理

    2024-04-10 18:54:02       12 阅读
  10. 为什么const声明的变量是可以修改的?

    2024-04-10 18:54:02       12 阅读
  11. 如何使用Python中的logging模块进行日志记录?

    2024-04-10 18:54:02       10 阅读
  12. AcWing 792. 高精度减法——算法基础课题解

    2024-04-10 18:54:02       13 阅读