当前位置: 首页 > news >正文

桥接模式案例

	桥接模式(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 {@Overridepublic void drawCircle(int radius, int x, int y) {System.out.println("用画笔绘制圆形,半径: " + radius + ", 位置: (" + x + ", " + y + ")");}@Overridepublic void drawRectangle(int width, int height, int x, int y) {System.out.println("用画笔绘制矩形,宽度: " + width + ", 高度: " + height + ", 位置: (" + x + ", " + y + ")");}
}// 画刷工具
public class Brush implements DrawingTool {@Overridepublic void drawCircle(int radius, int x, int y) {System.out.println("用画刷绘制圆形,半径: " + radius + ", 位置: (" + x + ", " + y + ")");}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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();}
}
http://www.lryc.cn/news/397728.html

相关文章:

  • Spring源码二十二:Bean实例化流程五
  • Unity3D中UI层级改变详解
  • centos安装数据库同步工具sqoop并导入数据,导出数据,添加定时任务
  • asp .net core 避免请求body数据量过大
  • 搭建discuz论坛(lvs+nginx+http+mysql+nfs)8台服务器
  • 就业平台小程序的设计
  • hid-ft260驱动学习笔记 5 - ft260_i2c_probe
  • Android上如何使用perfetto分析systrace
  • React Hooks学习笔记
  • BGP第二日
  • rabbitmq集群创建admin用户之后,提示can access virtual hosts是No access状态
  • ARM功耗管理之多核处理器启动
  • java使用easypoi模版导出word详细步骤
  • Android 内部保持数据的方式
  • uniapp 表格,动态表头表格封装渲染
  • beyond Compare连接 openWrt 和 VsCode
  • 量化机器人能否识别市场机会?
  • 香橙派AIpro开发板评测:部署yolov5模型实现图像和视频中物体的识别
  • MongoDB教程(二):mongoDB引用shell
  • A133 Android10 root修改
  • 实验场:在几分钟内使用 Bedrock Anthropic Models 和 Elasticsearch 进行 RAG 实验
  • 代理详解之静态代理、动态代理、SpringAOP实现
  • Laravel - laravel-websockets 开发详解
  • vue3 学习笔记04 -- axios的使用及封装
  • 键盘快捷键设置录入
  • 刷题Day49|647. 回文子串、516.最长回文子序列
  • 关于transformers库验证时不进入compute_metrics方法的一些坑
  • 苹果提出RLAIF:轻量级语言模型编写代码
  • [leetcode] shortest-subarray-with-sum-at-least-k 和至少为 K 的最短子数组
  • 专业140+总分420+天津大学815信号与系统考研经验天大电子信息与通信工程,真题,大纲,参考书。