2. 目标精通--用java写设计模式:接口隔离原则(Interface Segregation Principle)

1. 基本介绍:

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

2. 实际用例

  • 创建接口interface1,其中有5个未实现的方法 operation1~5
  • 创建类A实现interface1中的5个接口
  • 创建类B实现interface1中的5个接口
  • 创建类C,类C中需要依赖interface1中的1,2,3接口
  • 创建类D,类D中需要依赖interface1中的1,3,4接口
  • 接下来,我们发现,不论是类C还是类D,调用interface的实现类A,B时,都会有用不到的方法
  • 例如类C,只需要1,2,3这三个接口,但是实现类A中却实现了1~5所有接口,这样就违背了接口隔离原则
public class Segregation {

    public static void main(String[] args) {
        C c = new C();
        A a = new A();
        c.depend1(a);
        c.depend2(a);
        c.depend3(a);

        D d = new D();
        B b = new B();
        d.depend1(b);
        d.depend4(b);
        d.depend5(b);
    }
    
}

interface interface1{
    void operation1();
    void operation2();
    void operation3();
    void operation4();
    void operation5();
}

/**
 * A 实现了interface1
 */
class A implements interface1{
    @Override
    public void operation1() {
        System.out.println("A 实现了operation1");
    }
    @Override
    public void operation2() {
        System.out.println("A 实现了operation2");
    }
    @Override
    public void operation3() {
        System.out.println("A 实现了operation3");
    }
    @Override
    public void operation4() {
        System.out.println("A 实现了operation4");
    }
    @Override
    public void operation5() {
        System.out.println("A 实现了operation5");
    }
}

/**
 * B 实现了interface1
 */
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");
    }
}

/**
 * 类C ,只需要使用interface1中的1、2、3这三个方法
 */
class C{
    public void depend1(interface1 interface1){
        interface1.operation1();
    }
    public void depend2(interface1 interface1){
        interface1.operation2();
    }
    public void depend3(interface1 interface1){
        interface1.operation3();
    }
}

/**
 * 类D ,只需要使用interface1中的1、4、5这三个方法
 */
class D{
    public void depend1(interface1 interface1){
        interface1.operation1();
    }
    public void depend4(interface1 interface4){
        interface4.operation4();
    }
    public void depend5(interface1 interface5){
        interface5.operation5();
    }
}

3. 用例改进

- 将interface1分解为interface1,2,3-

  • 将interface1,通过细粒度拆分成interface1,interface2,interface3
  • A实现interface1与interface2
  • B实现interface1与interface3
  • 这样就可以将多余的方法剔除出去,C在使用A调用时,A里面只实现了1,2,3这三个方法,没有多余的4,5这两个方法,这样就实现了接口隔离原则
public class Segregation{
    public static void main(String[] args) {
        C c = new C();
        A a = new A();
        c.depend1(a);
        c.depend2(a);
        c.depend3(a);

        D d = new D();
        B b = new B();
        d.depend1(b);
        d.depend4(b);
        d.depend5(b);
    }
}
interface interface1{
    void operation1();

}
interface interface2{
    void operation2();
    void operation3();
}

interface interface3{
    void operation4();
    void operation5();
}
/**
 * A 实现了interface1
 */
class A implements interface1 ,interface2{
    @Override
    public void operation1() {
        System.out.println("A 实现了operation1");
    }
    @Override
    public void operation2() {
        System.out.println("A 实现了operation2");
    }
    @Override
    public void operation3() {
        System.out.println("A 实现了operation3");
    }
}



/**
 * B 实现了interface1
 */
class B implements interface1 ,interface3{
    @Override
    public void operation1() {
        System.out.println("B 实现了operation1");
    }
    @Override
    public void operation4() {
        System.out.println("B 实现了operation4");
    }
    @Override
    public void operation5() {
        System.out.println("B 实现了operation5");
    }
}

/**
 * 类C ,只需要使用interface1中的1、2、3这三个方法
 */
class C{
    public void depend1(interface1 interface1){
        interface1.operation1();
    }
    public void depend2(interface2 interface1){
        interface1.operation2();
    }
    public void depend3(interface2 interface1){
        interface1.operation3();
    }
}

/**
 * 类D ,只需要使用interface1中的1、4、5这三个方法
 */
class D{
    public void depend1(interface1 interface1){
        interface1.operation1();
    }
    public void depend4(interface3 interface4){
        interface4.operation4();
    }
    public void depend5(interface3 interface5){
        interface5.operation5();
    }
}

4. 下一章:依赖倒置原则

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值