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

接口隔离原则(Interface Segregation Principle)

基本介绍:

1.客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上。
在这里插入图片描述
虚线空心三角号 实现

虚线箭头 依赖

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

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

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

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

/**
 * 定义一个接口  有五个方法
 */
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分别与它们需要的接口建立依赖关系,也就是采用接口隔离原则。

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

拆分后的类图
在这里插入图片描述
拆分后的代码

/**
 * @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();
    }
}

输出

B 实现了operation1
B 实现了operation2
B 实现了operation2
D 实现了operation1
D 实现了operation4
D 实现了operation5

本文学习借鉴了尚硅谷老师的视频
此处

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值