对桥接模式的理解

目录

一、背景

  • 在《对装饰器模式的理解》中,当我们想基于某个类做扩展时,我们不是采用继承而是组合的方式。这样,可以避免子类爆炸的问题。
    • 想象一下俄罗斯套娃,类与类之间的结果也类似。
  • 还有一种场景:
    • 如果采用继承的方式,例如,类型AB1、类型AB2等,也会导致子类爆炸。
    • 那怎么解决呢? --> 依然是组合

二、桥接模式的demo

1、类型A(形状类型)

java 复制代码
public interface Shape {
    void draw();
}

public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("circle");
    }
}

public class Square implements Shape {
    @Override
    public void draw() {
        System.out.println("square");
    }
}

2、类型B(颜色类型)

java 复制代码
public interface Color {
    void fill();
}

public class RedColor implements Color {
    @Override
    public String fill() {
        return "red";
    }
}

public class BlueColor implements Color {
    @Override
    public String fill() {
       return "blue";
    }
}

3、需求:类型A要使用类型B(如:红色的方形)

  • 将类型A和类型B桥接起来。(本质上就是组合)

装饰器模式,是同一接口(同一抽象)的组合。

桥接模式,是不同接口(不同抽象)的组合。

  • 实现:
java 复制代码
public class Circle implements Shape {
    private Color color;

    public Circle(Color color) {
        this.color = color;
    }

    @Override
    public void draw() {
        System.out.println(color.fill() + " circle");
    }
}

public class Square implements Shape {
    private Color color;

    public Square(Color color) {
        this.color = color;
    }

    @Override
    public void draw() {
//        System.out.println("square");
        System.out.println(color.fill() + " square");
    }
}
java 复制代码
public class Application {
    public static void main(String[] args) {
        Shape redCircle = new Circle(new RedColor());
        redCircle.draw();

        Shape blueCircle = new Circle(new BlueColor());
        blueCircle.draw();

        System.out.println("-------------------------------");

        Square redSquare = new Square(new RedColor());
        redSquare.draw();

        Square blueSquare = new Square(new BlueColor());
        blueSquare.draw();
    }
}

4、Spring的方式

实际开发中,我们希望Spring帮我们组织好bean,我们拿来就用即可。

java 复制代码
@Configuration
public class ShapeConfig {
    @Bean
    public Color blueColor() {
        return new BlueColor();
    }

    @Bean
    public Color redColor() {
        return new RedColor();
    }

    @Bean
    public Square blueSquare(@Qualifier("blueColor") Color blueColor) {
        return new Square(blueColor);
    }

    @Bean
    public Square redSquare(@Qualifier("redColor") Color redColor) {
        return new Square(redColor);
    }

    @Bean
    public Circle blueCircle(@Qualifier("blueColor") Color blueColor) {
        return new Circle(blueColor);
    }

    @Bean
    public Circle redCircle(@Qualifier("redColor") Color redColor) {
        return new Circle(redColor);
    }
}
java 复制代码
@ComponentScan
public class Application {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Application.class);
        Circle blueCircle = applicationContext.getBean("blueCircle", Circle.class);
        blueCircle.draw();

        Circle redCircle = applicationContext.getBean("redCircle", Circle.class);
        redCircle.draw();

        System.out.println("--------------------------------------------------------------------------------");
        Square blueSquare = applicationContext.getBean("blueSquare", Square.class);
        blueSquare.draw();

        Square redSquare = applicationContext.getBean("redSquare", Square.class);
        redSquare.draw();

        applicationContext.close();
    }
}
相关推荐
后端AI实验室7 分钟前
等保三级整改,敏感数据加密,数十个系统——3个人用Cursor一周搞定了
java·ai
qq_334060219 分钟前
spring_springmvc_mybatis权限控制+boostrap实现UI
java·spring·mybatis
sunwenjian88615 分钟前
Spring Boot 整合 Druid 并开启监控
java·spring boot·后端
1104.北光c°20 分钟前
基于Canal + Kafka的高可用关注系统:一主多从关系链
java·开发语言·笔记·分布式·程序人生·kafka·一主多从
Mem0rin21 分钟前
[Java]异常及其处理
java·开发语言
skiy22 分钟前
Spring boot创建时常用的依赖
java·spring boot·后端
早起的年轻人24 分钟前
告别Git仓库臃肿:一招解决Maven target目录误提交问题
java·git·maven
快乐柠檬不快乐30 分钟前
Java连接电科金仓数据库(KingbaseES)实战指南
java·开发语言·数据库
程序员清风32 分钟前
看完Anthropic研究才懂:你有多会问,AI就有多强!
java·后端·面试
爱学习的小囧33 分钟前
VCF 集群部署灵活组合:单节点与高可用配置完全指南
java·服务器·前端