桥接模式

结构型模式

访问次数: 18

桥接模式将抽象部分与它的实现部分分离,使它们都可以独立地变化。

类图
classDiagram
    class Abstraction {
        -implementor: Implementor
        +operation()
    }
    class RefinedAbstraction {
        +operation()
    }
    class Implementor {
        <<interface>>
        +operationImpl()
    }
    class ConcreteImplementorA {
        +operationImpl()
    }
    class ConcreteImplementorB {
        +operationImpl()
    }
    Abstraction --> Implementor
    RefinedAbstraction --|> Abstraction
    Implementor <|-- ConcreteImplementorA
    Implementor <|-- ConcreteImplementorB
源代码示例
// 实现者接口
interface Implementor {
    void operationImpl();
}

// 具体实现者A
class ConcreteImplementorA implements Implementor {
    @Override
    public void operationImpl() {
        System.out.println("ConcreteImplementorA operation");
    }
}

// 具体实现者B
class ConcreteImplementorB implements Implementor {
    @Override
    public void operationImpl() {
        System.out.println("ConcreteImplementorB operation");
    }
}

// 抽象类
abstract class Abstraction {
    protected Implementor implementor;
    
    public Abstraction(Implementor implementor) {
        this.implementor = implementor;
    }
    
    public void operation() {
        implementor.operationImpl();
    }
}

// 扩展抽象类
class RefinedAbstraction extends Abstraction {
    public RefinedAbstraction(Implementor implementor) {
        super(implementor);
    }
    
    @Override
    public void operation() {
        System.out.println("RefinedAbstraction operation");
        super.operation();
    }
}
# Python代码待添加
应用场景
当需要在运行时切换实现,或者避免抽象和实现之间的永久绑定时。应用场景包括: 1)图形绘制系统中,形状(抽象)和绘制方式(实现)可以独立变化,支持不同的渲染引擎 2)数据库访问层,数据访问接口(抽象)和具体数据库实现(MySQL、Oracle、PostgreSQL)可以独立变化 3)消息发送系统,消息发送接口(抽象)和具体发送方式(邮件、短信、推送)可以独立变化 4)日志记录系统,日志接口(抽象)和具体记录方式(文件、数据库、网络)可以独立变化 5)缓存系统,缓存接口(抽象)和具体存储方式(内存、Redis、Memcached)可以独立变化 6)支付系统,支付接口(抽象)和具体支付方式(支付宝、微信、银行卡)可以独立变化
精选场景详解 —— 消息通知:发送抽象与通道实现分离
问题背景

通知既有「验证码 / 营销 / 告警」等业务类型,又有「短信 / 邮件 / 推送」等通道。若用继承两两组合,类数量会爆炸(类型×通道)。

模式如何解决
选用桥接模式:Notification(抽象)持有 MessageSender(实现)引用;SmsSender、EmailSender、PushSender 为具体实现。业务子类如 AlertNotification、MarketingNotification 可独立扩展。 两个维度独立变化: - 抽象维:通知业务形态; - 实现维:投递通道。 运行时还可切换 sender。 与策略模式相近但动机不同:桥接强调「抽象与实现解耦并长期并行扩展」;策略强调「可替换算法」。
场景模型(角色映射)

将模式中的抽象角色映射到该业务领域的具体类:

classDiagram
    class Notification {
        <<abstract>>
        #sender: MessageSender
        +notify(user: User, content: String)
    }
    class AlertNotification {
        +notify(user: User, content: String)
    }
    class MarketingNotification {
        +notify(user: User, content: String)
    }
    class MessageSender {
        <<interface>>
        +send(to: String, content: String)
    }
    class SmsSender {
        +send(to: String, content: String)
    }
    class EmailSender {
        +send(to: String, content: String)
    }
    class PushSender {
        +send(to: String, content: String)
    }
    Notification <|-- AlertNotification
    Notification <|-- MarketingNotification
    MessageSender <|.. SmsSender
    MessageSender <|.. EmailSender
    MessageSender <|.. PushSender
    Notification --> MessageSender