Java描述設計模式(08):橋接模式

1、橋接模式簡介

一、基礎描述

橋樑模式是對象的結構模式。又稱爲柄體(Handle and Body)模式或接口(Interface)模式。橋樑模式的用意是「將抽象化(Abstraction)與實現化(Implementation)脫耦,使得兩者能夠獨立地變化」。

二、場景問題描述

1)、場景分析git

在一個複雜的系統中,消息通知是一個必備模塊,通常封裝方式主要從下面兩個方式入手:github

  • 消息類型
用戶端消息(user-client)
管理端消息(system-client)
  • 消息接收
郵件發送(email)
短信發送(msg)

2)、場景圖解
圖片描述數據庫

3)、源碼實現架構

/**
 * 橋接模式場景應用
 */
public class C01_InScene {
    public static void main(String[] args) {
        // 建立具體的實現對象
        MsgImplementor implementor = new SendBySMS() ;
        // 建立普通的消息對象
        AbstractMsg abstractMessage = new UserMsg(implementor);
        abstractMessage.sendMessage("您的帳戶異地登錄", "用戶A0001");
        // 切換爲郵件方式且加急處理
        implementor = new SendByEmail() ;
        abstractMessage = new AdminMsg(implementor);
        abstractMessage.sendMessage("項目上線通知", "運維S0001");
    }
}
/**
 * 封裝消息類型
 */
abstract class AbstractMsg {
    // 持有一個實現部分的對象
    MsgImplementor impl ;
    public AbstractMsg (MsgImplementor impl){
        this.impl = impl ;
    }
    /**
     * 發送消息,委派給實現部分的方法
     * @param message    要發送消息的內容
     * @param toUser    消息的接受者
     */
    public void sendMessage (String message, String toUser){
        this.impl.send(message, toUser);
    }
}
class AdminMsg extends AbstractMsg{
    public AdminMsg(MsgImplementor impl) {
        super(impl);
    }
    @Override
    public void sendMessage(String message, String toUser) {
        message = "辛苦的管理員:"+message;
        super.sendMessage(message, toUser);
    }
}
class UserMsg extends AbstractMsg{
    public UserMsg(MsgImplementor impl) {
        super(impl);
    }
    @Override
    public void sendMessage(String message, String toUser) {
        message = "尊敬的用戶:" + message ;
        super.sendMessage(message, toUser);
    }
}

/**
 * 封裝消息發送
 */
interface MsgImplementor {
    void send (String message , String toUser) ;
}
class SendBySMS implements MsgImplementor{
    @Override
    public void send(String message, String toUser) {
        System.out.println("短信通知:"+toUser+";內容:"+message);
    }
}
class SendByEmail implements MsgImplementor{
    @Override
    public void send(String message, String toUser) {
        System.out.println("郵件通知:"+toUser+";內容:"+message);
    }
}

2、橋接模式

一、模式圖解

圖片描述

二、核心角色

  • 抽象化(Abstraction)角色
抽象化給出的定義,並保存一個對實現化對象的引用。
  • 修正抽象化(RefinedAbstraction)角色
擴展抽象化角色,改變修正父類對抽象化的定義。
  • 實現化(Implementor)角色
這個角色給出實現化角色的接口,但不給出具體的實現。
  • 具體實現化(ConcreteImplementor)角色
這個角色給出實現化角色接口的具體實現。

三、源碼封裝

abstract class Abstraction {
    private Implementor implementor  ;
    public Abstraction (Implementor implementor){
        this.implementor = implementor ;
    }
    // 實例方法
    public void operation (){
        implementor.operationImpl();
    }
}
class RefinedAbstraction extends Abstraction{
    public RefinedAbstraction(Implementor implementor) {
        super(implementor);
    }
    //其餘的操做方法
    public void otherOperation(){
    }
}
abstract class Implementor {
    // 示例方法,實現抽象部分須要的某些具體功能
    public abstract void operationImpl () ;
}
class ConcreteImplementorA extends Implementor{
    @Override
    public void operationImpl() {
        System.out.println("ConcreteImplementorA.operationImpl()");
    }
}
class ConcreteImplementorB extends Implementor{
    @Override
    public void operationImpl() {
        System.out.println("ConcreteImplementorB.operationImpl()");
    }
}

3、Java應用場景

橋樑模式在Java應用中的一個很是典型的例子就是JDBC驅動器。抽象API能夠對各類數據庫引擎發出SQL指令,並不直接與數據庫引擎互動,JDBC驅動器負責這個底層的工做。

圖片描述

JDBC的這種架構,把抽象部分和具體部分分離開來,從而使得抽象部分和具體部分均可以獨立地擴展。

4、源代碼地址

GitHub地址:知了一笑
https://github.com/cicadasmile/model-arithmetic-parent
碼雲地址:知了一笑
https://gitee.com/cicadasmile/model-arithmetic-parent

圖片描述

相關文章
相關標籤/搜索