23.结构型 - 桥接模式(Bridge)

1.定义

  • 将抽象部分与实现部分分离,使其可以独立的变化
  • 前提是当前类有两个独立变化的维度,且两个维度都需要进行扩展

2.UML类图

在这里插入图片描述

  • 角色说明
    Abstraction : 抽象部分(维度一),持有实现部分(维度二)的引用
    RefinedAbstraction : 优化的实现部分(示例代码中LargeCoffee, SmallCoffee)
    Implementor : 实现部分 (示例中AbstractAdditive)
    ConcreteImplA : 具体实现A
    ConcreteImplB : 具体实现B
  • 要点说明
    一个类具有两个维度,且两个维度都需要扩展,请用桥接模式

3.UML示例代码

/**
 * Copyright (C), 2016-2020
 * FileName: AbstractCoffee
 * Author: wei.zheng
 * Date: 2020/1/14 8:18
 * Description: Coffee抽象类(维度一),持有抽象Additive的引用
 */
public abstract class AbstractCoffee {
    protected AbstractAdditive impl;

    public AbstractCoffee(AbstractAdditive impl) {
        this.impl = impl;
    }

    public abstract void makeCoffee();
}
/**
 * Copyright (C), 2016-2020
 * FileName: LargeCoffee
 * Author: wei.zheng
 * Date: 2020/1/14 8:22
 * Description: 大杯咖啡
 */
public class LargeCoffee extends AbstractCoffee {
    public LargeCoffee(AbstractAdditive impl) {
        super(impl);
    }

    @Override
    public void makeCoffee() {
        System.out.println("Large " + impl.addAdditive() + " coffee");
    }
}
/**
 * Copyright (C), 2016-2020
 * FileName: SmallCoffee
 * Author: wei.zheng
 * Date: 2020/1/14 8:23
 * Description: 小杯咖啡
 */
public class SmallCoffee extends AbstractCoffee {
    public SmallCoffee(AbstractAdditive impl) {
        super(impl);
    }

    @Override
    public void makeCoffee() {
        System.out.println("Small " + impl.addAdditive() + " coffee");
    }
}
/**
 * Copyright (C), 2016-2020
 * FileName: AbstractAdditive
 * Author: wei.zheng
 * Date: 2020/1/14 8:20
 * Description: 添加物抽象类(维度二),即优化的抽象部分
 */
public abstract class AbstractAdditive {
    public abstract String addAdditive();
}
/**
 * Copyright (C), 2016-2020
 * FileName: Milk
 * Author: wei.zheng
 * Date: 2020/1/14 8:25
 * Description: 牛奶添加物
 */
public class Milk extends AbstractAdditive {
    @Override
    public String addAdditive() {
        return "milk";
    }
}
/**
 * Copyright (C), 2016-2020
 * FileName: Sugar
 * Author: wei.zheng
 * Date: 2020/1/14 8:25
 * Description: 糖添加物
 */
public class Sugar extends AbstractAdditive {
    @Override
    public String addAdditive() {
        return "Sugar";
    }
}
/**
 * Copyright (C), 2016-2020
 * FileName: Client
 * Author: wei.zheng
 * Date: 2020/1/14 8:27
 * Description: 桥接模式用户类
 */
public class Client {
    public static void main(String[] args) {
        AbstractAdditive milk = new Milk();
        AbstractAdditive sugar = new Sugar();

        // 大杯加奶
        LargeCoffee largeCoffeeMilk = new LargeCoffee(milk);
        largeCoffeeMilk.makeCoffee();

        // 小杯加糖
        SmallCoffee smallCoffeeSugar = new SmallCoffee(sugar);
        smallCoffeeSugar.makeCoffee();
    }
}
// 运行结果
2020-01-14 08:35:01.142 21380-21380/com.example.bridge I/System.out: Large milk coffee
2020-01-14 08:35:01.142 21380-21380/com.example.bridge I/System.out: Small Sugar coffee

4.总结

桥接模式,可以对多维度变化的类进行解耦,使各个维度独立变化,互不影响。

发布了37 篇原创文章 · 获赞 0 · 访问量 557

猜你喜欢

转载自blog.csdn.net/qq_37514242/article/details/103967138
今日推荐