桥接模式案例

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

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

  1. 定义实现部分的接口
    首先,我们定义一个绘制工具的接口,这个接口将作为桥接模式的实现部分。
java 复制代码
// 绘制工具接口
public interface DrawingTool {
    void drawCircle(int radius, int x, int y);
    void drawRectangle(int width, int height, int x, int y);
}
  1. 实现具体的绘制工具
    接下来,我们实现具体的绘制工具,如画笔和画刷。
java 复制代码
// 画笔工具
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. 定义抽象部分的接口
    接下来,我们定义一个图形的抽象类,这个抽象类将作为桥接模式的抽象部分。
java 复制代码
// 图形抽象类
public abstract class Shape {
    protected DrawingTool drawingTool;

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

    public abstract void draw();
}
  1. 实现具体的图形
    然后,我们实现具体的图形,如圆形和矩形。
java 复制代码
// 圆形
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. 使用桥接模式
    最后,我们使用桥接模式来绘制图形。
java 复制代码
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();
    }
}
相关推荐
Ustinian_31033 分钟前
【C/C++】For 循环展开与性能优化【附代码讲解】
c语言·开发语言·c++
牵牛老人43 分钟前
Qt 插件开发全解析:从接口定义,插件封装,插件调用到插件间的通信
开发语言·qt
杨荧1 小时前
基于Python的反诈知识科普平台 Python+Django+Vue.js
大数据·前端·vue.js·python·数据分析
钮钴禄·爱因斯晨1 小时前
AIGC浪潮下,风靡全球的Mcp到底是什么?一文讲懂,技术小白都知道!!
开发语言·人工智能·深度学习·神经网络·生成对抗网络·aigc
大模型真好玩1 小时前
深入浅出LangChain AI Agent智能体开发教程(九)—LangChain从0到1搭建知识库
人工智能·python·mcp
22jimmy2 小时前
JavaWeb(二)CSS
java·开发语言·前端·css·入门·基础
机器视觉知识推荐、就业指导4 小时前
面试问题详解五:Qt 信号与槽的动态管理
开发语言·qt
MZ_ZXD0016 小时前
springboot汽车租赁服务管理系统-计算机毕业设计源码58196
java·c++·spring boot·python·django·flask·php
A 计算机毕业设计-小途6 小时前
大四零基础用Vue+ElementUI一周做完化妆品推荐系统?
java·大数据·hadoop·python·spark·毕业设计·毕设
念念010710 小时前
数学建模竞赛中评价类相关模型
python·数学建模·因子分析·topsis