对桥接模式的理解

目录

一、背景

  • 在《对装饰器模式的理解》中,当我们想基于某个类做扩展时,我们不是采用继承而是组合的方式。这样,可以避免子类爆炸的问题。
    • 想象一下俄罗斯套娃,类与类之间的结果也类似。
  • 还有一种场景:
    • 如果采用继承的方式,例如,类型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();
    }
}
相关推荐
better_liang23 分钟前
每日Java面试场景题知识点之-XXL-JOB分布式任务调度实践
java·spring boot·xxl-job·分布式任务调度·企业级开发
会游泳的石头25 分钟前
一行注解防死循环:MyBatis 递归深度限制(无需 level 字段)
java·mybatis
q***o37626 分钟前
Spring Boot环境配置
java·spring boot·后端
oMcLin28 分钟前
如何在SUSE Linux Enterprise Server 15 SP4上通过配置并优化ZFS存储池,提升文件存储与数据备份的效率?
java·linux·运维
TaiKuLaHa42 分钟前
Spring Bean的生命周期
java·后端·spring
刀法如飞1 小时前
开箱即用的 DDD(领域驱动设计)工程脚手架,基于 Spring Boot 4.0.1 和 Java 21
java·spring boot·mysql·spring·设计模式·intellij-idea
我是苏苏1 小时前
Web开发:C#通过ProcessStartInfo动态调用执行Python脚本
java·服务器·前端
JavaGuide1 小时前
SpringBoot 官宣停止维护 3.2.x~3.4.x!
java·后端
tkevinjd2 小时前
动态代理
java