前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >23种设计模式之接口隔离原则

23种设计模式之接口隔离原则

作者头像
暴躁的程序猿
发布2022-03-23 17:07:17
2000
发布2022-03-23 17:07:17
举报

接口隔离原则(Interface Segregation Principle)

基本介绍:

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

在这里插入图片描述
在这里插入图片描述

虚线空心三角号 实现

虚线箭头 依赖

我们可以这里 类B实现了接口的全部方法 类D实现了接口的全部方法

但是类A只使用类B的一二三方法

类B只使用类D的一四五方法

我们按照当前类图情况编写出来代码

代码语言:javascript
复制
/**
 * 定义一个接口  有五个方法
 */
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");
    }
}

/**
 * D类实现了 Interface1
 */
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类通过接口interface1 依赖(使用)B类,但是只会用到1,2,3方法
 */
class A {
    public void depend1(Interface1 interface1){
        interface1.operation1();
    }
    public void depend2(Interface1 interface1){
        interface1.operation2();
    }
    public void depend3(Interface1 interface1){
        interface1.operation3();
    }
}

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

我们可以发现 不是每个方法都被使用 我们实现那么多方法没有用到

这里按照我们的接口隔离原则 应该把接口拆分成几个独立的接口 类A和类C分别与它们需要的接口建立依赖关系,也就是采用接口隔离原则。

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

拆分后的类图

在这里插入图片描述
在这里插入图片描述

拆分后的代码

代码语言:javascript
复制
/**
 * @create: 2021/9/25
 * @author: Tony Stark
 */
public class Segregation1 {
    public static void main(String[] args) {
        A a = new A();
        //a通过接口依赖B类
        a.depend1(new B());
        a.depend2(new B());
        a.depend2(new B());

        C c = new C();
        //c类通过接口依赖D类
        c.depend1(new D());
        c.depend4(new D());
        c.depend5(new D());
    }
}

/**
 * 接口1
 */
interface Interface1 {
    void operation1();
}

/**
 * 接口2
 */
interface Interface2{
    void operation2();
    void operation3();
}

/**
 * 接口3
 */
interface Interface3{
    void operation4();
    void operation5();
}

class B implements Interface1,Interface2 {

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

/**
 * D类实现了 Interface1
 */
class D implements Interface1,Interface3 {

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

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

        interface2.operation3();
    }
}

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

输出

代码语言:javascript
复制
B 实现了operation1
B 实现了operation2
B 实现了operation2
D 实现了operation1
D 实现了operation4
D 实现了operation5
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-09-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 接口隔离原则(Interface Segregation Principle)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档