08 桥接模式

本文介绍了桥接模式在软件设计中的应用,尤其是在汽车部件构成示例中,如何通过将颜色、形状和材料等维度分离到接口,避免了继承导致的子类数量膨胀,保持了代码结构的清晰和可扩展性。
摘要由CSDN通过智能技术生成

桥接模式

桥接模式 使用场景,存在多个纬度的变化,每个纬度都需要进行扩展 使用继承的话会导致子类发生裂变,数量极具膨胀。 使用桥接模式的话这能避免这种现象。使子类线性膨胀。 比如我们由一个汽车,他由许多的部件构成,比如外形、颜色 我们有2个接口,一个外形接口,一个颜色接口,每次实现都继承这两个接口。 外形每多一个,就会多颜色数量的车辆。总的类型数量是 外形的数量 * 颜色的数量 实现类的数量也是以上那么多。 而使用桥接模式 , 总的数量不变,但是实现类的数量却变为了 外形的数量 + 颜色的数量。
在这里插入图片描述

demo

package structuralpattern;

/**
 * @author tx
 * @version 1.0
 * @date 2024/1/7 22:26
 * @description:
 * 桥接模式
 * 使用场景,存在多个纬度的变化,每个纬度都需要进行扩展
 * 使用继承的话会导致子类发生裂变,数量极具膨胀。
 * 使用桥接模式的话这能避免这种现象。使子类线性膨胀。
 * 比如我们由一个汽车,他由许多的部件构成,比如外形、颜色
 * 我们有2个接口,一个外形接口,一个颜色接口,每次实现都继承这两个接口。
 * 外形每多一个,就会多颜色数量的车辆。总的类型数量是 外形的数量 * 颜色的数量
 * 实现类的数量也是以上那么多。
 *
 * 而使用桥接模式 ,
 * 总的数量不变,但是实现类的数量却变为了 外形的数量 + 颜色的数量。
 */
public class BridgePattern {
    public static void main(String[] args) {
        // 获取一个轮胎,牌子是NB,颜色是蓝色,形状是圆形,材料是PC
        Wheel wheel = new NB(new Blue(),new Circle(),new PC());
        // 获取一个轮胎,牌子是红山,颜色是红色,形状是方向,材料是LT
        Wheel wheel1 = new Redhill(new Red(),new Square(),new LT());
        /**
         * 后面的三个参数可以任意搭配,
         * 这样就将各个维度然后构成的轮胎由指数变为线性,
         * 避免类的膨胀。
         */
        wheel.show();
        wheel1.show();
    }
}

/**
 * 轮子的抽象层,
 * 简单使用模板方法定义轮子的构成的打印顺序
 */
abstract class Wheel{
    // 颜色
    private final Color color;
    // 形状
    private final Shape shape;
    // 材料
    private final Material material;
    private String brand;

    public Wheel(Color color, Shape shape, Material material) {
        this.color = color;
        this.shape = shape;
        this.material = material;
    }
    public void show(){
        brand = this.brandShow();
        System.out.println(brand+"车轮配置:");
        color.printColor();
        shape.printShape();
        material.printMaterial();
    }

    /**
     * 车轮的品牌
     */
    protected abstract String brandShow();
}
/**
 * 具体的车轮 NB牌
 */
class NB extends Wheel {
    public NB(Color color, Shape shape, Material material) {
        super(color, shape, material);
    }

    /**
     * 车轮的品牌
     */
    @Override
    protected String brandShow() {
        return "牛B牌";
    }

}
class Redhill extends Wheel {
    public Redhill(Color color, Shape shape, Material material) {
        super(color, shape, material);
    }

    /**
     * 车轮的品牌
     */
    @Override
    protected String brandShow() {
        return "红山牌";
    }

}

/**
 * 颜色接口 --------------------------
 */
interface Color{
    /**
     * 打印颜色
     */
    void printColor();
}
class Red implements Color{

    /**
     * 打印颜色
     */
    @Override
    public void printColor() {
        System.out.println("红色");
    }
}

class Blue implements Color{

    /**
     * 打印颜色
     */
    @Override
    public void printColor() {
        System.out.println("蓝色");
    }
}

/**
 * 形状 --------------------------
 */
interface Shape{
    /**
     * 打印形状
     */
    void printShape();
}
class Circle implements Shape{

    /**
     * 打印形状
     */
    @Override
    public void printShape() {
        System.out.println("圆形");
    }
}

class Square implements Shape{

    /**
     * 打印形状
     */
    @Override
    public void printShape() {
        System.out.println("正方形");
    }
}
/**
 * 材料 --------------------------
 */
interface Material{
    /**
     * 打印材料
     */
    void printMaterial();
}
class PC implements Material{
    /**
     * 打印材料
     */
    @Override
    public void printMaterial() {
        System.out.println("PC材料");
    }
}
class LT implements Material{
    /**
     * 打印材料
     */
    @Override
    public void printMaterial() {
        System.out.println("LT材料");
    }
}

demo打印
在这里插入图片描述

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值