• 每天一个设计模式之桥接模式


    这是一个让人很迷惑的模式,这里所谓的“桥接”,按照GoF的说法,是对“抽象”和“实现”的桥接,让两个可以各自独立变化而互不影响,但是对于这个抽象的理解让人摸不着头脑。更多见到的例子大家都是用来表示一个对象朝着两个不同的维度各自变化,这里面就体现不到所谓的“抽象”。在实际的各种框架中也很少能见到这个模式的应用,感觉这个模式不是那么地典型。

    一、UML类图

    在这里插入图片描述
    这个模式体现了组合优于继承的良好实践,继承式的实现应该是下面这样
    在这里插入图片描述
    而组合式的实现则是下面这样,
    在这里插入图片描述
    可以看到这里Shape扮演Abstraction角色,Color扮演Implementor角色,这里的关键点在于Shape对Color的引用。

    二、代码示例

    Abstraction

    public abstract class Shape {
        protected Colorful colorful;
    
        public Shape(Colorful c) {
            this.colorful = c;
        }
    
        abstract protected void applyColor();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    RefinedAbstractions

    public class Circle extends Shape {
        public Circle(Colorful c) {
            super(c);
        }
    
        protected void applyColor() {
            colorful.applyColor();
            System.out.println("Circle filled with color @" + colorful.getColor());
        }
    }
    
    public class Cube extends Shape {
        public Cube(Colorful c) {
            super(c);
        }
    
        protected void applyColor() {
            colorful.applyColor();
            System.out.println("Cube filled with color @" + colorful.getColor());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    Implementor

    public interface Colorful {
        void applyColor();
        String getColor();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    ConcreteImplementor

    public class PurpleColor implements Colorful {
        public void applyColor() {
            System.out.println(getColor() + " applied ==> ");
        }
    
        public String getColor() {
            return "purple";
        }
    }
    
    public class GreenColor implements Colorful {
        public void applyColor() {
            System.out.println(getColor() + " applied ==> ");
        }
    
        public String getColor() {
            return "green";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    客户代码

    public class Client {
        public static void main(String[] args) {
            Shape purpleCircle = new Circle(new PurpleColor());
            Shape greenCircle = new Circle(new GreenColor());
            purpleCircle.applyColor();
            greenCircle.applyColor();
    
            System.out.println("-------------------------------");
    
            Shape purpleCube = new Cube(new PurpleColor());
            Shape greenCube = new Cube(new GreenColor());
            purpleCube.applyColor();
            greenCube.applyColor();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    运行结果

    purple applied ==> 
    Circle filled with color @purple
    green applied ==> 
    Circle filled with color @green
    -------------------------------
    purple applied ==> 
    Cube filled with color @purple
    green applied ==> 
    Cube filled with color @green
    
    Process finished with exit code 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    三、总结

    桥接模式的优点: 1、抽象和实现的分离。 2、优秀的扩展能力。 3、实现细节对客户透明。
    这个模式可以实现两个变化维度(形状、颜色)随意地扩展、随意地组合,降低了不同部件之间的耦合性,提高了代码的扩展性。

    【参考】

    1. https://refactoringguru.cn/design-patterns/bridge
    2. https://www.runoob.com/design-pattern/bridge-pattern.html
    3. https://www.digitalocean.com/community/tutorials/bridge-design-pattern-java
    4. https://www.baeldung.com/java-bridge-pattern
    5. https://www.geeksforgeeks.org/bridge-design-pattern/
  • 相关阅读:
    Springboot: Spring Cloud Gateway 使用的基本概念及配置介绍
    Python高级语法----Python C扩展与性能优化
    数据库之架构
    Kubernetes (K8s) 解读:微服务与容器编排的未来
    LeetCode 628. 三个数的最大乘积
    人工智能迷惑行为大赏
    FlexRay通信协议概述
    【教3妹学算法-每日3题(1)】检查单词是否为句中其他单词的前缀
    CANoe-激活总线状态、CAPL导航器不显示Test Case视图
    ASP.NET Core 6框架揭秘实例演示[08]:配置的基本编程模式
  • 原文地址:https://blog.csdn.net/M_sdn/article/details/126290364