对桥接模式的理解

目录

一、背景

  • 在《对装饰器模式的理解》中,当我们想基于某个类做扩展时,我们不是采用继承而是组合的方式。这样,可以避免子类爆炸的问题。
    • 想象一下俄罗斯套娃,类与类之间的结果也类似。
  • 还有一种场景:
    • 如果采用继承的方式,例如,类型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();
    }
}
相关推荐
骑士雄师25 分钟前
Java 泛型中级面试题及答案
java·开发语言·面试
.格子衫.6 小时前
Spring Boot 原理篇
java·spring boot·后端
多云几多6 小时前
Yudao单体项目 springboot Admin安全验证开启
java·spring boot·spring·springbootadmin
Jabes.yang9 小时前
Java求职面试实战:从Spring Boot到微服务架构的技术探讨
java·数据库·spring boot·微服务·面试·消息队列·互联网大厂
聪明的笨猪猪9 小时前
Java Redis “高可用 — 主从复制”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
兮动人9 小时前
Spring Bean耗时分析工具
java·后端·spring·bean耗时分析工具
MESSIR229 小时前
Spring IOC(控制反转)中常用注解
java·spring
摇滚侠9 小时前
Spring Boot 3零基础教程,Demo小结,笔记04
java·spring boot·笔记
笨手笨脚の10 小时前
设计模式-迭代器模式
java·设计模式·迭代器模式·行为型设计模式
spencer_tseng11 小时前
Eclipse 4.7 ADT (Android Development Tools For Eclipse)
android·java·eclipse