接口隔离原则_接口隔离原则【typescript】

基本介绍

1) 客户端不应该依赖它不需要的接

口,即一个类对另一个类的依赖

应该建立在最小的接口上

2) 先看一张图:

732d68edee195097efe377274c609129.png

3)

类A通过接口Interface1依赖类B,类C通过

接口Interface1依赖类D,如果接口

Interface1对于类A和类C来说不是最小接口,

那么类B和类D必须去实现他们不需要的方

法。

4)

按隔离原则应当这样处理:

将接口Interface1拆分为独立的几个接口,

类A和类C分别与他们需要的接口建立依赖

关系。也就是采用接口隔离原则

应用实例

1) 类A通过接口Interface1依赖类B,

类C通过接口Interface1依赖类D,

请编写代码完成此应用实例。

a457e725b12bf1149544ad2c6b1e7f91.png

代码

namespace Segregation1 {
    /** 1.类A通过接口Interface1依赖类B,类C通过
        接口Interface1依赖类D,如果接口
        Interface1对于类A和类C来说不是最小接口,
        那么类B和类D必须去实现他们不需要的方
        法。
        2.按隔离原则应当这样处理:
        将接口Interface1拆分为独立的几个接口,
        类A和类C分别与他们需要的接口建立依赖
        关系。也就是采用接口隔离原则
        */
    interface Interface1 {
        opreation1(): void;
        opreation2(): void;
        opreation3(): void;
        opreation4(): void;
        opreation5(): void;
    }

    class B implements Interface1 {
        opreation1(): void {
            console.log('B 实现了 operation1');
        }
        opreation2(): void {
            console.log('B 实现了 operation2');
        }
        opreation3(): void {
            console.log('B 实现了 operation3');
        }
        opreation4(): void {
            console.log('B 实现了 operation4');
        }
        opreation5(): void {
            console.log('B 实现了 operation5');
        }
    }

    class D implements Interface1 {
        opreation1(): void {
            console.log('D 实现了 operation1');
        }
        opreation2(): void {
            console.log('D 实现了 operation2');
        }
        opreation3(): void {
            console.log('D 实现了 operation3');
        }
        opreation4(): void {
            console.log('D 实现了 operation4');
        }
        opreation5(): void {
            console.log('D 实现了 operation5');
        }
    }

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

    //C类通过接口Interface1依赖(使用)B类,但是只会用到1,4,5方法
    class C {
        depend1(i: Interface1): void {
            i.opreation1();
        }
        depend4(i: Interface1): void {
            i.opreation4();
        }
        depend5(i: Interface1): void {
            i.opreation5();
        }
    }
}

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

1) 类A通过接口Interface1依赖类B,类C通过接口Interface1依赖类D,如果接口

Interface1对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不

需要的方法

2) 将接口Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立

依赖关系。也就是采用接口隔离原则

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

254df91e56a7b52222a27f4579f9e136.png

4) 代码实现

namespace Segregation1 {
    /** 1.类A通过接口Interface1依赖类B,类C通过
        接口Interface1依赖类D,如果接口
        Interface1对于类A和类C来说不是最小接口,
        那么类B和类D必须去实现他们不需要的方
        法。
        2.按隔离原则应当这样处理:
        将接口Interface1拆分为独立的几个接口,
        类A和类C分别与他们需要的接口建立依赖
        关系。也就是采用接口隔离原则
        */
    //接口1
    interface Interface1 {
        opreation1(): void;
    }
    //接口2
    interface Interface2 {
        opreation2(): void;
        opreation3(): void;
    }
    //接口3
    interface Interface3 {
        opreation4(): void;
        opreation5(): void;
    }

    class B implements Interface1, Interface2 {
        opreation1(): void {
            console.log('B 实现了 operation1');
        }
        opreation2(): void {
            console.log('D 实现了 operation2');
        }
        opreation3(): void {
            console.log('D 实现了 operation3');
        }
    }

    class D implements Interface1, Interface3 {
        opreation1(): void {
            console.log('D 实现了 operation1');
        }
        opreation4(): void {
            console.log('D 实现了 operation4');
        }
        opreation5(): void {
            console.log('D 实现了 operation5');
        }
    }

    //A类通过接口Interface1,Interface2依赖(使用)B类,但是只会用到1,2,3方法
    class A {
        depend1(i: Interface1): void {
            i.opreation1();
        }
        depend2(i: Interface2): void {
            i.opreation2();
        }
        depend3(i: Interface2): void {
            i.opreation3();
        }
    }

    //C类通过接口Interface1,Interface3依赖(使用)B类,但是只会用到1,4,5方法
    class C {
        depend1(i: Interface1): void {
            i.opreation1();
        }
        depend4(i: Interface3): void {
            i.opreation4();
        }
        depend5(i: Interface3): void {
            i.opreation5();
        }
    }

    let a = new A();
    a.depend1(new B());//A类通过接口去依赖B类
    a.depend2(new B());
    a.depend3(new B());

    let c = new C();//C类通过接口D去依赖D类
    c.depend1(new D());
    c.depend4(new D());
    c.depend5(new D());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值