Java結(jié)構(gòu)型設(shè)計模式之橋接模式詳細(xì)講解
橋接模式
概述
橋接模式(Bridge Pattern)也稱為橋梁模式、接口(Interfce)模式或柄體(Handle and Body)模式,屬于結(jié)構(gòu)型模式。
它是將抽象部分與它的具體實現(xiàn)部分分離,使它們都可以獨立地變化。
橋接模式主要目的是通過組合的方式建立兩個類之間的聯(lián)系,而不是繼承。橋接模式的核心在于解耦抽象和實現(xiàn)。

應(yīng)用場景
當(dāng)一個類內(nèi)部具備兩種或多種變化維度時,使用橋接模式可以解耦這些變化的維度,使高層代碼架構(gòu)隱定。
適用業(yè)務(wù)場景:
1.在抽象和具體實現(xiàn)之間需要增加更多的靈活性的場景。
2.一個類存在兩個(或多個)獨立變化的維度,而這兩個(或多個)維度都需要獨立進(jìn)行擴展。
3.不希望使用繼承,或因為多層繼承導(dǎo)致系統(tǒng)類的個數(shù)劇增。
優(yōu)缺點
優(yōu)點:
1.分離抽象部分及其具體實現(xiàn)部分
2.提高了系統(tǒng)的擴展性
3.實現(xiàn)細(xì)節(jié)對客戶透明
缺點:
1.增加了系統(tǒng)的理解與設(shè)計難度
2.需要正確地識別系統(tǒng)中兩個獨立變化的維度
主要角色
1.抽象(Abstraction)
該類持有一個對實現(xiàn)角色的引用,抽象角色中的方法需要實現(xiàn)角色來實現(xiàn)。抽象角色一般為抽象類(構(gòu)造函數(shù)規(guī)定子類要傳入一個實現(xiàn)對象)。
2.修正抽象(RefinedAbstraction)
抽象的具體實現(xiàn),對抽象的方法進(jìn)行完善和擴展。
3.實現(xiàn)(Implementor)
確定實現(xiàn)維度的基本操作,提供給抽象使用。該類一般為接口或抽象類。
1.具體實現(xiàn)(Concretelmplementor)
實現(xiàn)的具體實現(xiàn)。

橋接模式的基本使用
創(chuàng)建實現(xiàn)角色
public interface IImplementor {
void operationImpl();
}
創(chuàng)建具體實現(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) {
// 來一個實現(xiàn)化角色
IImplementor impA = new ConcreteImplementorA();
IImplementor impB = new ConcreteImplementorB();
// 來一個抽象化角色,聚合實現(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
橋接模式實現(xiàn)消息發(fā)送
使用橋接模式解耦消息類型與消息重要程度。
創(chuàng)建實現(xiàn)角色
創(chuàng)建實現(xiàn)角色,擔(dān)任橋接角色,實現(xiàn)消息發(fā)送的統(tǒng)一接口
public interface IMessage {
void send(String message);
}
創(chuàng)建具體實現(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)任橋接抽象角色,持有實現(xiàn)角色,且發(fā)送消息委派給實現(xiàn)對象發(fā)送
public abstract class AbastractMessage {
// 持有實現(xiàn)角色的引用
private IMessage message;
// 構(gòu)造函數(shù),傳入實現(xiàn)角色的引用
public AbastractMessage(IMessage message) {
this.message = message;
}
// 發(fā)送消息的方法,調(diào)用實現(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è)計模式之橋接模式詳細(xì)講解的文章就介紹到這了,更多相關(guān)Java橋接模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java SpringBoot在RequestBody中高效的使用枚舉參數(shù)原理案例詳解
這篇文章主要介紹了Java SpringBoot在RequestBody中高效的使用枚舉參數(shù)原理案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
Eclipse+Java+Swing實現(xiàn)圖書管理系統(tǒng)(詳細(xì)代碼)
這篇文章主要介紹了Eclipse+Java+Swing實現(xiàn)圖書管理系統(tǒng)并附上詳細(xì)代碼,需要的小伙伴可以參考一下,希望對你有所幫助2022-01-01
基于Springboot疫苗接種行程管理系統(tǒng)的設(shè)計與實現(xiàn)
本文主要介紹了基于Springboot實現(xiàn)的疫苗接種行程管理系統(tǒng)的示例代碼,系統(tǒng)主要實現(xiàn)個人疫苗接種管理、行程管理、病史管理、風(fēng)險地區(qū)管理、核酸檢測報告結(jié)果上報、疫情新聞管理等功能,需要的可以參考一下2022-03-03
Java中BigDecimal,DateFormatter?和迭代器的"陷阱"
這篇文章主要介紹了Java中BigDecimal,DateFormatter?和迭代器的"陷阱",文章圍繞主題展開詳細(xì)的內(nèi)容介紹,感興趣的小伙伴可以參考一下2022-06-06
springboot oauth2實現(xiàn)單點登錄實例
我們見過的很多網(wǎng)站,容許使用第三方賬號登錄,oauth2是用來做三方登錄的,本文就詳細(xì)的介紹springboot oauth2實現(xiàn)單點登錄實例,具有一定的參考價值,感興趣的可以了解一下2022-01-01

