对桥接模式的理解

目录

一、背景

  • 在《对装饰器模式的理解》中,当我们想基于某个类做扩展时,我们不是采用继承而是组合的方式。这样,可以避免子类爆炸的问题。
    • 想象一下俄罗斯套娃,类与类之间的结果也类似。
  • 还有一种场景:
    • 如果采用继承的方式,例如,类型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();
    }
}
相关推荐
KingDol_MIni19 分钟前
Spring Boot 集成 T-io 实现客户端服务器通信
java·服务器·spring boot
许苑向上23 分钟前
Java八股文(下)
java·开发语言
逸Y 仙X28 分钟前
Git常见命令--助力开发
java·大数据·git·java-ee·github·idea
独孤求败Ace31 分钟前
第44天:Web开发-JavaEE应用&反射机制&类加载器&利用链&成员变量&构造方法&抽象方法
java·开发语言
FLZJ_KL32 分钟前
【设计模式】【创建型模式】单例模式(Singleton)
java·单例模式·设计模式
CL_IN39 分钟前
企业数据集成:实现高效调拨出库自动化
java·前端·自动化
计算机-秋大田44 分钟前
基于Spring Boot的农产品智慧物流系统设计与实现(LW+源码+讲解)
java·开发语言·spring boot·后端·spring·课程设计
计算机毕设指导61 小时前
基于SpringBoot的城乡商城协作系统【附源码】
java·spring boot·后端·mysql·spring·tomcat·maven
华子w9089258591 小时前
基于数据可视化+SpringBoot+安卓端的数字化施工项目计划与管理平台设计和实现
java·spring boot·后端
橘猫云计算机设计1 小时前
基于Django的购物商城平台的设计与实现(源码+lw+部署文档+讲解),源码可白嫖!
java·数据库·spring boot·后端·django