Java結(jié)構(gòu)型設(shè)計(jì)模式之橋接模式詳細(xì)講解
橋接模式
概述
橋接模式(Bridge Pattern)也稱為橋梁模式、接口(Interfce)模式或柄體(Handle and Body)模式,屬于結(jié)構(gòu)型模式。
它是將抽象部分與它的具體實(shí)現(xiàn)部分分離,使它們都可以獨(dú)立地變化。
橋接模式主要目的是通過(guò)組合的方式建立兩個(gè)類之間的聯(lián)系,而不是繼承。橋接模式的核心在于解耦抽象和實(shí)現(xiàn)。
應(yīng)用場(chǎng)景
當(dāng)一個(gè)類內(nèi)部具備兩種或多種變化維度時(shí),使用橋接模式可以解耦這些變化的維度,使高層代碼架構(gòu)隱定。
適用業(yè)務(wù)場(chǎng)景:
1.在抽象和具體實(shí)現(xiàn)之間需要增加更多的靈活性的場(chǎng)景。
2.一個(gè)類存在兩個(gè)(或多個(gè))獨(dú)立變化的維度,而這兩個(gè)(或多個(gè))維度都需要獨(dú)立進(jìn)行擴(kuò)展。
3.不希望使用繼承,或因?yàn)槎鄬永^承導(dǎo)致系統(tǒng)類的個(gè)數(shù)劇增。
優(yōu)缺點(diǎn)
優(yōu)點(diǎn):
1.分離抽象部分及其具體實(shí)現(xiàn)部分
2.提高了系統(tǒng)的擴(kuò)展性
3.實(shí)現(xiàn)細(xì)節(jié)對(duì)客戶透明
缺點(diǎn):
1.增加了系統(tǒng)的理解與設(shè)計(jì)難度
2.需要正確地識(shí)別系統(tǒng)中兩個(gè)獨(dú)立變化的維度
主要角色
1.抽象(Abstraction)
該類持有一個(gè)對(duì)實(shí)現(xiàn)角色的引用,抽象角色中的方法需要實(shí)現(xiàn)角色來(lái)實(shí)現(xiàn)。抽象角色一般為抽象類(構(gòu)造函數(shù)規(guī)定子類要傳入一個(gè)實(shí)現(xiàn)對(duì)象)。
2.修正抽象(RefinedAbstraction)
抽象的具體實(shí)現(xiàn),對(duì)抽象的方法進(jìn)行完善和擴(kuò)展。
3.實(shí)現(xiàn)(Implementor)
確定實(shí)現(xiàn)維度的基本操作,提供給抽象使用。該類一般為接口或抽象類。
1.具體實(shí)現(xiàn)(Concretelmplementor)
實(shí)現(xiàn)的具體實(shí)現(xiàn)。
橋接模式的基本使用
創(chuàng)建實(shí)現(xiàn)角色
public interface IImplementor { void operationImpl(); }
創(chuàng)建具體實(shí)現(xiàn)角色
public class ConcreteImplementorA implements IImplementor { public void operationImpl() { System.out.println("ConcreteImplementorA operationImpl"); } } public class ConcreteImplementorB implements IImplementor { public void operationImpl() { System.out.println("ConcreteImplementorB operationImpl"); } }
創(chuàng)建抽象角色
public abstract class Abstraction { protected IImplementor mImplementor; public Abstraction(IImplementor implementor) { this.mImplementor = implementor; } public void operation() { this.mImplementor.operationImpl(); } }
創(chuàng)建修正抽象角色
public class RefinedAbstraction extends Abstraction { public RefinedAbstraction(IImplementor implementor) { super(implementor); } @Override public void operation() { super.operation(); System.out.println("RefinedAbstraction operation"); } }
客戶端調(diào)用
public static void main(String[] args) { // 來(lái)一個(gè)實(shí)現(xiàn)化角色 IImplementor impA = new ConcreteImplementorA(); IImplementor impB = new ConcreteImplementorB(); // 來(lái)一個(gè)抽象化角色,聚合實(shí)現(xiàn) Abstraction absA = new RefinedAbstraction(impA); Abstraction absB = new RefinedAbstraction(impB); // 執(zhí)行操作 absA.operation(); absB.operation(); }
ConcreteImplementorA operationImpl
RefinedAbstraction operation
ConcreteImplementorB operationImpl
RefinedAbstraction operation
橋接模式實(shí)現(xiàn)消息發(fā)送
使用橋接模式解耦消息類型與消息重要程度。
創(chuàng)建實(shí)現(xiàn)角色
創(chuàng)建實(shí)現(xiàn)角色,擔(dān)任橋接角色,實(shí)現(xiàn)消息發(fā)送的統(tǒng)一接口
public interface IMessage { void send(String message); }
創(chuàng)建具體實(shí)現(xiàn)角色
public class EmailMessage implements IMessage { @Override public void send(String message) { System.out.println("使用郵件消息發(fā)送" + message); } } public class SmsMessage implements IMessage { @Override public void send(String message) { System.out.println("使用短信消息發(fā)送" + message); } }
創(chuàng)建抽象角色
創(chuàng)建抽象角色,擔(dān)任橋接抽象角色,持有實(shí)現(xiàn)角色,且發(fā)送消息委派給實(shí)現(xiàn)對(duì)象發(fā)送
public abstract class AbastractMessage { // 持有實(shí)現(xiàn)角色的引用 private IMessage message; // 構(gòu)造函數(shù),傳入實(shí)現(xiàn)角色的引用 public AbastractMessage(IMessage message) { this.message = message; } // 發(fā)送消息的方法,調(diào)用實(shí)現(xiàn)角色的方法 void sendMessage(String message){ this.message.send(message); } }
創(chuàng)建修正抽象角色
創(chuàng)建普通消息
public class NomalMessage extends AbastractMessage { public NomalMessage(IMessage message) { super(message); } }
創(chuàng)建重要消息
public class ImportanceMessage extends AbastractMessage { public ImportanceMessage(IMessage message) { message = message + "【重要消息】"; super(message); } void sendMessage(String message){ super.sendMessage(message); } }
客戶端調(diào)用
public static void main(String[] args) { IMessage message = new EmailMessage(); AbastractMessage abastractMessage = new NomalMessage(message); abastractMessage.sendMessage("hello world"); message = new SmsMessage(); abastractMessage = new ImportanceMessage(message); abastractMessage.sendMessage("hello world"); }
使用郵件消息發(fā)送hello world
使用短信消息發(fā)送hello world【重要消息】
到此這篇關(guān)于Java結(jié)構(gòu)型設(shè)計(jì)模式之橋接模式詳細(xì)講解的文章就介紹到這了,更多相關(guān)Java橋接模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA2020.3創(chuàng)建web工程的完整步驟
這篇文章主要給大家介紹了關(guān)于IDEA2020.3創(chuàng)建web工程的完整步驟,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01RabbitMQ交換機(jī)使用場(chǎng)景和消息可靠性總結(jié)分析
這篇文章主要為大家介紹了RabbitMQ交換機(jī)使用場(chǎng)景和消息可靠性總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01通過(guò)springboot+mybatis+druid配置動(dòng)態(tài)數(shù)據(jù)源
這篇文章主要介紹了通過(guò)springboot+mybatis+druid配置動(dòng)態(tài)數(shù)據(jù)源,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06基于Java實(shí)現(xiàn)的Base64加密、解密原理代碼
這篇文章主要介紹了基于Java實(shí)現(xiàn)的Base64加密、解密原理代碼,需要的朋友可以參考下2014-07-07SpringBoot整合Hutool實(shí)現(xiàn)文件上傳的使用示例
文件上傳在項(xiàng)目經(jīng)常會(huì)用到,本文主要介紹了SpringBoot整合Hutool實(shí)現(xiàn)文件上傳的使用示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11如何在spring boot中進(jìn)行參數(shù)校驗(yàn)示例詳解
這篇文章主要介紹了如何在spring-boot中進(jìn)行參數(shù)校驗(yàn)及l(fā)ombok的使用詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05IntelliJ IDEA失焦自動(dòng)重啟服務(wù)的解決方法
在使用 IntelliJ IDEA運(yùn)行 SpringBoot 項(xiàng)目時(shí),你可能會(huì)遇到一個(gè)令人困擾的問(wèn)題,一旦你的鼠標(biāo)指針離開當(dāng)前IDE窗口,點(diǎn)擊其他位置時(shí), IDE 窗口會(huì)失去焦點(diǎn),你的 SpringBoot 服務(wù)就會(huì)自動(dòng)重啟,所以本文給大家介紹了IntelliJ IDEA失焦自動(dòng)重啟服務(wù)的解決方法2023-10-10