桥接模式案例

	桥接模式(Bridge Pattern)是一种结构型设计模式,它将抽象部分与实现部分分离,使它们可以独立变化。桥接模式通过创
建一个桥接接口,将抽象部分和实现部分连接起来,从而实现两者的解耦。

下面是一个详细的桥接模式案例,假设我们要设计一个图形绘制系统,支持不同类型的图形(如圆形、矩形)和不同的绘制工具(如画笔、画刷)。

  1. 定义实现部分的接口
    首先,我们定义一个绘制工具的接口,这个接口将作为桥接模式的实现部分。
// 绘制工具接口
public interface DrawingTool {
    void drawCircle(int radius, int x, int y);
    void drawRectangle(int width, int height, int x, int y);
}

  1. 实现具体的绘制工具
    接下来,我们实现具体的绘制工具,如画笔和画刷。
// 画笔工具
public class Pen implements DrawingTool {
    @Override
    public void drawCircle(int radius, int x, int y) {
        System.out.println("用画笔绘制圆形,半径: " + radius + ", 位置: (" + x + ", " + y + ")");
    }

    @Override
    public void drawRectangle(int width, int height, int x, int y) {
        System.out.println("用画笔绘制矩形,宽度: " + width + ", 高度: " + height + ", 位置: (" + x + ", " + y + ")");
    }
}

// 画刷工具
public class Brush implements DrawingTool {
    @Override
    public void drawCircle(int radius, int x, int y) {
        System.out.println("用画刷绘制圆形,半径: " + radius + ", 位置: (" + x + ", " + y + ")");
    }

    @Override
    public void drawRectangle(int width, int height, int x, int y) {
        System.out.println("用画刷绘制矩形,宽度: " + width + ", 高度: " + height + ", 位置: (" + x + ", " + y + ")");
    }
}

  1. 定义抽象部分的接口
    接下来,我们定义一个图形的抽象类,这个抽象类将作为桥接模式的抽象部分。
// 图形抽象类
public abstract class Shape {
    protected DrawingTool drawingTool;

    public Shape(DrawingTool drawingTool) {
        this.drawingTool = drawingTool;
    }

    public abstract void draw();
}

  1. 实现具体的图形
    然后,我们实现具体的图形,如圆形和矩形。
// 圆形
public class Circle extends Shape {
    private int radius;
    private int x;
    private int y;

    public Circle(int radius, int x, int y, DrawingTool drawingTool) {
        super(drawingTool);
        this.radius = radius;
        this.x = x;
        this.y = y;
    }

    @Override
    public void draw() {
        drawingTool.drawCircle(radius, x, y);
    }
}

// 矩形
public class Rectangle extends Shape {
    private int width;
    private int height;
    private int x;
    private int y;

    public Rectangle(int width, int height, int x, int y, DrawingTool drawingTool) {
        super(drawingTool);
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
    }

    @Override
    public void draw() {
        drawingTool.drawRectangle(width, height, x, y);
    }
}

  1. 使用桥接模式
    最后,我们使用桥接模式来绘制图形。
public class BridgePatternDemo {
    public static void main(String[] args) {
        Shape circleWithPen = new Circle(10, 50, 50, new Pen());
        circleWithPen.draw();

        Shape circleWithBrush = new Circle(10, 50, 50, new Brush());
        circleWithBrush.draw();

        Shape rectangleWithPen = new Rectangle(20, 30, 100, 100, new Pen());
        rectangleWithPen.draw();

        Shape rectangleWithBrush = new Rectangle(20, 30, 100, 100, new Brush());
        rectangleWithBrush.draw();
    }
}

相关推荐

  1. 模式案例

    2024-07-11 17:42:05       24 阅读
  2. 设计模式

    2024-07-11 17:42:05       38 阅读
  3. [go] 模式

    2024-07-11 17:42:05       62 阅读
  4. 模式简介

    2024-07-11 17:42:05       41 阅读
  5. 模式

    2024-07-11 17:42:05       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 17:42:05       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 17:42:05       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 17:42:05       58 阅读
  4. Python语言-面向对象

    2024-07-11 17:42:05       69 阅读

热门阅读

  1. C++八股(一)

    2024-07-11 17:42:05       21 阅读
  2. springmvc前端jsp与html

    2024-07-11 17:42:05       20 阅读
  3. 前端不同年限的差异不同开发经验的差异

    2024-07-11 17:42:05       22 阅读
  4. 锂电池容量低,原因何在?

    2024-07-11 17:42:05       20 阅读
  5. IEC62056标准体系简介-5.低层通信协议

    2024-07-11 17:42:05       21 阅读
  6. (补充)IDEA项目结构

    2024-07-11 17:42:05       20 阅读
  7. nest通过装饰器获取jwt有效负载数据

    2024-07-11 17:42:05       25 阅读
  8. 物联网设计竞赛_10_Jetson Nano中文转汉语语音

    2024-07-11 17:42:05       20 阅读
  9. C++中的设计模式

    2024-07-11 17:42:05       23 阅读
  10. 软考中项报名需要什么条件?全方位分析!

    2024-07-11 17:42:05       19 阅读
  11. vscode编辑keil工程 5. vscode 提交git一直卡着转圈圈

    2024-07-11 17:42:05       20 阅读