设计模式之桥接模式(Java)-JDBC也实现了桥接模式

发布于:2025-06-30 ⋅ 阅读:(19) ⋅ 点赞:(0)

桥接模式

桥接模式是一种结构型设计模式,它将抽象部分与实现部分分离,使它们可以独立变化。解决组合爆炸问题。

直接上案例,假设一个需要根据不同绘图API绘画不同的形状时。

定义api接口以及他的实现类

/**
 * 实现部分接口 - 绘图API
 */
interface DrawingAPI {
    void drawCircle(double x, double y, double radius);
    void drawRectangle(double x1, double y1, double x2, double y2);
}

/**
 * 具体实现 - 栅格绘图API
 */
class RasterDrawingAPI implements DrawingAPI {
    @Override
    public void drawCircle(double x, double y, double radius) {
        System.out.printf("在栅格图形上绘制圆: (%.1f, %.1f) 半径 %.1f\n", x, y, radius);
    }
    
    @Override
    public void drawRectangle(double x1, double y1, double x2, double y2) {
        System.out.printf("在栅格图形上绘制矩形: 从(%.1f, %.1f) 到 (%.1f, %.1f)\n", 
                         x1, y1, x2, y2);
    }
}


/**
 * 具体实现 - 矢量绘图API
 */
class VectorDrawingAPI implements DrawingAPI {
    @Override
    public void drawCircle(double x, double y, double radius) {
        System.out.printf("在矢量图形上绘制圆: (%.1f, %.1f) 半径 %.1f\n", x, y, radius);
    }
    
    @Override
    public void drawRectangle(double x1, double y1, double x2, double y2) {
        System.out.printf("在矢量图形上绘制矩形: 从(%.1f, %.1f) 到 (%.1f, %.1f)\n", 
                         x1, y1, x2, y2);
    }
}

然后再抽象一个形状,以及继承形状的抽象类

/**
 * 抽象部分 - 形状
 */
abstract class Shape {
    protected DrawingAPI drawingAPI;
    
    protected Shape(DrawingAPI drawingAPI) {
        this.drawingAPI = drawingAPI;
    }
    
    public abstract void draw();     // 低层次操作
    public abstract void resize(double scale); // 高层次操作
}

/**
 * 扩展抽象部分 - 圆形
 */
class Circle extends Shape {
    private double x, y, radius;
    
    public Circle(double x, double y, double radius, DrawingAPI drawingAPI) {
        super(drawingAPI);
        this.x = x;
        this.y = y;
        this.radius = radius;
    }
    
    @Override
    public void draw() {
        drawingAPI.drawCircle(x, y, radius);
    }
    
    @Override
    public void resize(double scale) {
        radius *= scale;
        System.out.printf("圆形缩放 %.1f 倍 -> 新半径: %.1f\n", scale, radius);
    }
}

/**
 * 扩展抽象部分 - 矩形
 */
class Rectangle extends Shape {
    private double x1, y1, x2, y2;
    
    public Rectangle(double x1, double y1, double x2, double y2, DrawingAPI drawingAPI) {
        super(drawingAPI);
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }
    
    @Override
    public void draw() {
        drawingAPI.drawRectangle(x1, y1, x2, y2);
    }
    
    @Override
    public void resize(double scale) {
        double width = x2 - x1;
        double height = y2 - y1;
        
        double newWidth = width * scale;
        double newHeight = height * scale;
        
        x2 = x1 + newWidth;
        y2 = y1 + newHeight;
        
        System.out.printf("矩形缩放 %.1f 倍 -> 新尺寸: %.1f x %.1f\n", 
                         scale, newWidth, newHeight);
    }
}

现在api、形状就定义完成了,写一个main测试一下

package com.zsy.Bridge;

/**
 * 桥接模式 Demo
 * 演示如何将图形形状(抽象部分)与绘图API(实现部分)解耦
 */
public class Main {
    public static void main(String[] args) {
        // 创建不同的绘图API(实现部分)
        DrawingAPI rasterAPI = new RasterDrawingAPI();
        DrawingAPI vectorAPI = new VectorDrawingAPI();

        // 创建不同的形状(抽象部分),并与绘图API桥接
        Shape circle1 = new Circle(1, 2, 3, rasterAPI);
        Shape circle2 = new Circle(5, 7, 11, vectorAPI);

        Shape rectangle1 = new Rectangle(1, 2, 4, 6, rasterAPI);
        Shape rectangle2 = new Rectangle(3, 5, 8, 13, vectorAPI);

        // 绘制形状
        System.out.println("使用不同的绘图API绘制形状:");
        circle1.draw();
        circle2.draw();
        rectangle1.draw();
        rectangle2.draw();

        System.out.println("\n调整大小并重新绘制:");
        circle1.resize(2.5);
        circle1.draw();

        rectangle2.resize(0.5);
        rectangle2.draw();
    }
}

输出::

可以发现他根据不同的API接口实现了不同形状的画法,这里我们将shape和api接口进行了联系,(提供具体的绘图实现(栅格或矢量)

  • 可以独立地选择形状和绘图API

  • 运行时动态组合抽象和实现部分

JDBC体现的桥接模式

首先这是他的大概工作流程。

// 1. 加载具体实现 (通常通过SPI自动加载,这里显式加载以展示关系)
Class.forName("com.mysql.jdbc.Driver"); // 加载MySQL驱动实现

// 2. 获取抽象接口
Connection conn = DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/mydb", 
    "user", 
    "password");

// 3. 使用抽象接口操作 (不关心具体实现)
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");

// 此时:
// - conn 实际是 MySQLConnection 实例
// - stmt 实际是 MySQLStatement 实例
// - rs 实际是 MySQLResultSet 实例
// 但应用程序只与抽象接口交互

1. 结构分析
角色 JDBC 对应组件 说明
抽象部分(Abstraction) java.sql.ConnectionStatement 等接口 定义数据库操作的抽象高层API
扩展抽象(Refined Abstraction) 具体数据库操作逻辑(如事务管理) 基于抽象接口扩展的功能
实现部分(Implementor) java.sql.Driver 接口 定义驱动需要实现的底层操作
具体实现(Concrete Implementor) MySQL Driver、Oracle Driver 等 各数据库厂商提供的具体驱动实现

协调了不同数据库与不同不同操作之间的关系,由于抽象了connection和statement,在实现drive可以根据不同的数据库厂商实现返回不同的连接。

// MySQL 的具体实现
public class Driver implements java.sql.Driver {
    public Connection connect(String url, Properties info) throws SQLException {
        // 返回MySQL的具体Connection实现
        return new MySQLConnection(url, info);
    }
    // 其他实现...
}

// Oracle 的具体实现
public class OracleDriver implements java.sql.Driver {
    public Connection connect(String url, Properties info) throws SQLException {
        // 返回Oracle的具体Connection实现
        return new OracleConnection(url, info);
    }
    // 其他实现...
}


网站公告

今日签到

点亮在社区的每一天
去签到