• 设计模式——装饰者模式、桥接模式、外观模式(结构型模式)


    1.装饰者模式

    装饰(Decorator)模式中的角色:

    • 抽象构件(Component)角色 :定义一个抽象接口以规范准备接收附加责任的对象。

    • 具体构件(Concrete Component)角色 :实现抽象构件,通过装饰角色为其添加一些职责。

    • 抽象装饰(Decorator)角色 : 继承或实现抽象构件,并包含具体构件的实例,可以通过其子类扩展具体构件的功能。

    • 具体装饰(ConcreteDecorator)角色 :实现抽象装饰的相关方法,并给具体构件对象添加附加的责任。

    抽象构件:

    1. //快餐:抽象构件
    2. public abstract class FastFood {
    3. private float price;//价格
    4. private String desc; //描述
    5. public float getPrice() {
    6. return price;
    7. }
    8. public void setPrice(float price) {
    9. this.price = price;
    10. }
    11. public String getDesc() {
    12. return desc;
    13. }
    14. public void setDesc(String desc) {
    15. this.desc = desc;
    16. }
    17. public FastFood(float price, String desc) {
    18. this.price = price;
    19. this.desc = desc;
    20. }
    21. public FastFood() {
    22. }
    23. public abstract float cost();//算钱
    24. }

    具体构件:

    1. //炒面:具体的构件角色
    2. public class FriedNoodles extends FastFood {
    3. public FriedNoodles() {
    4. super(12,"炒面");
    5. }
    6. public float cost() {
    7. return getPrice();
    8. }
    9. }
    10. //炒饭:具体构件角色
    11. public class FriedRice extends FastFood {
    12. public FriedRice() {
    13. super(10,"炒饭");
    14. }
    15. public float cost() {
    16. return getPrice();
    17. }
    18. }

    抽象装饰:

    1. //抽象装饰角色
    2. public abstract class Garnish extends FastFood {
    3. //声明快餐类的变量
    4. private FastFood fastFood;
    5. //有参构造方法
    6. public Garnish(FastFood fastFood, float price, String desc) {
    7. super(price, desc);
    8. this.fastFood = fastFood;
    9. }
    10. public FastFood getFastFood() {
    11. return fastFood;
    12. }
    13. public void setFastFood(FastFood fastFood) {
    14. this.fastFood = fastFood;
    15. }
    16. }

    具体装饰:

    1. //鸡蛋类:具体的装饰者角色
    2. public class Egg extends Garnish {
    3. public Egg(FastFood fastFood) {
    4. super(fastFood,1,"鸡蛋");
    5. }
    6. public float cost() {
    7. //计算价格
    8. return getPrice() + getFastFood().cost();
    9. }
    10. @Override
    11. public String getDesc() {
    12. return super.getDesc() + getFastFood().getDesc();
    13. }
    14. }
    15. //培根类:具体的装饰者角色
    16. public class Bacon extends Garnish {
    17. public Bacon(FastFood fastFood) {
    18. super(fastFood,2,"培根");
    19. }
    20. public float cost() {
    21. //计算价格
    22. return getPrice() + getFastFood().cost();
    23. }
    24. @Override
    25. public String getDesc() {
    26. return super.getDesc() + getFastFood().getDesc();
    27. }
    28. }

    测试类:

    1. //测试类
    2. public class Client {
    3. public static void main(String[] args) {
    4. //点一份炒饭
    5. FastFood food = new FriedRice();
    6. System.out.println(food.getDesc() + " " + food.cost() + "元");//炒饭 10.0元
    7. System.out.println("===============");
    8. //在上面的炒饭中加一个鸡蛋
    9. food = new Egg(food);
    10. System.out.println(food.getDesc() + " " + food.cost() + "元");//鸡蛋炒饭 11.0元
    11. System.out.println("================");
    12. //再加一个鸡蛋
    13. food = new Egg(food);
    14. System.out.println(food.getDesc() + " " + food.cost() + "元");//鸡蛋鸡蛋炒饭 12.0元
    15. System.out.println("================");
    16. food = new Bacon(food);
    17. System.out.println(food.getDesc() + " " + food.cost() + "元");//培根鸡蛋鸡蛋炒饭 14.0元
    18. }
    19. }

    2.桥接模式

    桥接(Bridge)模式包含以下主要角色:

    • 抽象化(Abstraction)角色 :定义抽象类,并包含一个对实现化对象的引用。

    • 扩展抽象化(Refined Abstraction)角色 :是抽象化角色的子类,实现父类中的业务方法,并通过组合关系调用实现化角色中的业务方法。

    • 实现化(Implementor)角色 :定义实现化角色的接口,供扩展抽象化角色调用。

    • 具体实现化(Concrete Implementor)角色 :给出实现化角色接口的具体实现。

    抽象化角色:

    1. //操作系统版本:抽象化角色
    2. public abstract class OperatingSystemVersion {
    3. protected VideoFile videoFile;
    4. public OperatingSystemVersion(VideoFile videoFile) {
    5. this.videoFile = videoFile;
    6. }
    7. public abstract void play(String fileName);
    8. }

    扩展抽象化角色:

    1. //Windows版本:扩展抽象化角色
    2. public class Windows extends OperatingSystemVersion {
    3. public Windows(VideoFile videoFile) {
    4. super(videoFile);
    5. }
    6. public void play(String fileName) {
    7. videoFile.decode(fileName);
    8. }
    9. }
    10. //mac版本:扩展抽象化角色
    11. public class Mac extends OperatingSystemVersion {
    12. public Mac(VideoFile videoFile) {
    13. super(videoFile);
    14. }
    15. public void play(String fileName) {
    16. videoFile.decode(fileName);
    17. }
    18. }

    实现化角色:

    1. //视频文件:实现化
    2. public interface VideoFile {
    3. void decode(String fileName);
    4. }

    具体实现化角色:

    1. //avi文件:具体实现化
    2. public class AVIFile implements VideoFile {
    3. public void decode(String fileName) {
    4. System.out.println("avi视频文件:"+ fileName);
    5. }
    6. }
    7. //rmvb文件:具体实现化
    8. public class REVBBFile implements VideoFile {
    9. public void decode(String fileName) {
    10. System.out.println("rmvb文件:" + fileName);
    11. }
    12. }

    测试类:

    1. //测试类
    2. public class Client {
    3. public static void main(String[] args) {
    4. OperatingSystemVersion os = new Windows(new AVIFile());
    5. os.play("战狼3");
    6. }
    7. }

    使用场景

    • 当一个类存在两个独立变化的维度,且这两个维度都需要进行扩展时。

    • 当一个系统不希望使用继承或因为多层次继承导致系统类的个数急剧增加时。

    • 当一个系统需要在构件的抽象化角色和具体化角色之间增加更多的灵活性时。避免在两个层次之间建立静态的继承联系,通过桥接模式可以使它们在抽象层建立一个关联关系。

    3.外观模式

    外观(Facade)模式包含以下主要角色:

    • 外观(Facade)角色:为多个子系统对外提供一个共同的接口。

    • 子系统(Sub System)角色:实现系统的部分功能,客户可以通过外观角色访问它。

    子系统角色:

    1. //灯类:子系统角色
    2. public class Light {
    3. public void on() {
    4. System.out.println("打开了灯....");
    5. }
    6. public void off() {
    7. System.out.println("关闭了灯....");
    8. }
    9. }
    10. //空调类:子系统角色
    11. public class AirCondition {
    12. public void on() {
    13. System.out.println("打开了空调....");
    14. }
    15. public void off() {
    16. System.out.println("关闭了空调....");
    17. }
    18. }
    19. //电视类:子系统角色
    20. public class TV {
    21. public void on() {
    22. System.out.println("打开了电视....");
    23. }
    24. public void off() {
    25. System.out.println("关闭了电视....");
    26. }
    27. }

    外观角色:

    1. //智能音箱:外观角色
    2. public class SmartAppliancesFacade {
    3. private Light light;
    4. private TV tv;
    5. private AirCondition airCondition;
    6. public SmartAppliancesFacade() {
    7. light = new Light();
    8. tv = new TV();
    9. airCondition = new AirCondition();
    10. }
    11. public void say(String message) {
    12. if(message.contains("打开")) {
    13. on();
    14. } else if(message.contains("关闭")) {
    15. off();
    16. } else {
    17. System.out.println("我还听不懂你说的!!!");
    18. }
    19. }
    20. //起床后一键开电器
    21. private void on() {
    22. System.out.println("起床了");
    23. light.on();
    24. tv.on();
    25. airCondition.on();
    26. }
    27. //睡觉一键关电器
    28. private void off() {
    29. System.out.println("睡觉了");
    30. light.off();
    31. tv.off();
    32. airCondition.off();
    33. }
    34. }

    测试类:

    1. //测试类
    2. public class Client {
    3. public static void main(String[] args) {
    4. //创建外观对象
    5. SmartAppliancesFacade facade = new SmartAppliancesFacade();
    6. //客户端直接与外观对象进行交互
    7. facade.say("打开家电");
    8. facade.say("关闭家电");
    9. }
    10. }

    好处:

    • 降低了子系统与客户端之间的耦合度,使得子系统的变化不会影响调用它的客户类。

    • 对客户屏蔽了子系统组件,减少了客户处理的对象数目,并使得子系统使用起来更加容易。

    缺点:

    • 不符合开闭原则,修改很麻烦

    使用场景

    • 对分层结构系统构建时,使用外观模式定义子系统中每层的入口点可以简化子系统之间的依赖关系。

    • 当一个复杂系统的子系统很多时,外观模式可以为系统设计一个简单的接口供外界访问。

    • 当客户端与多个子系统之间存在很大的联系时,引入外观模式可将它们分离,从而提高子系统的独立性和可移植性。

  • 相关阅读:
    sql函数coalesce和parse_url
    力扣 6080. 使数组按非递减顺序排列
    代码随想录算法训练营第五十九天|647. 回文子串、 516.最长回文子序列
    机器学习中遇到的函数以及用法
    12.LoadRunner,基于html录制和基于url录制
    数据结构--平衡二叉树
    SAT Encoding and CDCL Algorithm听课笔记
    【QT开发笔记-基础篇】| 第四章 事件QEvent | 4.6 定时器事件
    heroku的使用,部署node后端
    torch lighting 为不同模型设置不同的学习率
  • 原文地址:https://blog.csdn.net/m0_63544124/article/details/126011110