Java設(shè)計(jì)模式之橋接模式實(shí)例詳解
本文實(shí)例講述了Java設(shè)計(jì)模式之橋接模式。分享給大家供大家參考,具體如下:
概念:
橋接模式(Bridge Pattern):將抽象部分與它的實(shí)現(xiàn)部分分離,使它們都可以獨(dú)立地變化。
橋接模式將繼承關(guān)系轉(zhuǎn)換為關(guān)聯(lián)關(guān)系,從而降低了類與類之間的耦合,減少了代碼編寫量。
什么情況下會(huì)用橋接模式?
簡(jiǎn)單的說(shuō)就是我們?cè)诔橄髮?duì)象的特征時(shí),對(duì)象的特征屬性又很抽象,不得不把屬性再次抽象。
否則的話,具體子類的數(shù)量將會(huì)成幾何增長(zhǎng),而且不易擴(kuò)展。沒(méi)辦法維護(hù)現(xiàn)有代碼。
舉例,我們?cè)诔橄笫謾C(jī)這二個(gè)對(duì)象時(shí),它的幾個(gè)屬性,如操作系統(tǒng),cpu,屏幕,運(yùn)營(yíng)商網(wǎng)絡(luò)等都很復(fù)雜。我們不能簡(jiǎn)單的把這幾個(gè)屬性直接定義,必須再次抽象化。而具體的一個(gè)手機(jī)對(duì)象就是這些屬性的組合,但不是簡(jiǎn)單的組合,屬性需要實(shí)現(xiàn)自己作為屬性的功能。在這樣的設(shè)計(jì)下,代碼的維護(hù)和擴(kuò)展也就容易了。
注意:在說(shuō)這個(gè)模式的時(shí)候,我不能保證說(shuō)的和寫得例子都是正確的,畢竟我也是新接觸到,所有例子均基于與個(gè)人理解。
我認(rèn)為的橋接模式說(shuō)明圖:

下面是例子:
1. 首先定義抽象類,抽象和描述對(duì)象的特征。
在對(duì)象的屬性上劃分維度,為了以后橋接和擴(kuò)展。
package test.design.bridge;
public abstract class CellPhone {
private String cellPhoneName;
public CellPhoneSystem cellPhoneSystem;
public CellPhoneCPU cellPhoneCPU;
public void works(){
System.out.println("---------------------");
System.out.println("This cellphone is:"+this.getCellPhoneName()+",welcome to use. ");
System.out.println("This cellphone detail infomation:");
System.out.println("系統(tǒng)類型:"+this.getCellPhoneSystem().getSystemName());
System.out.println("cpu型號(hào):"+this.getCellPhoneCPU().getCpuName());
System.out.println("---------------------");
}
public String getCellPhoneName() {
return cellPhoneName;
}
public void setCellPhoneName(String cellPhoneName) {
this.cellPhoneName = cellPhoneName;
}
public CellPhoneSystem getCellPhoneSystem() {
return cellPhoneSystem;
}
public void setCellPhoneSystem(CellPhoneSystem cellPhoneSystem) {
this.cellPhoneSystem = cellPhoneSystem;
}
public CellPhoneCPU getCellPhoneCPU() {
return cellPhoneCPU;
}
public void setCellPhoneCPU(CellPhoneCPU cellPhoneCPU) {
this.cellPhoneCPU = cellPhoneCPU;
}
}
2. 屬性維度的抽象。(可以使用接口定義,關(guān)鍵看你的具體功能)
package test.design.bridge;
/**
* 屬性cpu被抽象成一個(gè)維度,為了以后擴(kuò)展
* @author lushuaiyin
*
*/
public abstract class CellPhoneCPU {
public CellPhone cellPhone;
public String cpuName;
public void cpuWorks(){
System.out.println("I am cpu. My pattern is:"+this.getCpuName());
System.out.println("I am working for this cellphone:"+this.getCellPhone().getCellPhoneName());
}
public CellPhone getCellPhone() {
return cellPhone;
}
public void setCellPhone(CellPhone cellPhone) {
this.cellPhone = cellPhone;
this.getCellPhone().setCellPhoneCPU(this);// 裝配(橋接,或者可以認(rèn)為對(duì)象類與其屬性類的傳遞)
}
public String getCpuName() {
return cpuName;
}
public void setCpuName(String cpuName) {
this.cpuName = cpuName;
}
}
package test.design.bridge;
/**
* 屬性操作系統(tǒng)被抽象成一個(gè)維度,為了以后擴(kuò)展
* @author lushuaiyin
*
*/
public abstract class CellPhoneSystem {
public CellPhone cellPhone;
public String SystemName;
public void systemWorks(){
System.out.println("I am "+this.getSystemName()+" system.");
System.out.println("I am working for this cellphone:"+this.getCellPhone().getCellPhoneName());
}
public CellPhone getCellPhone() {
return cellPhone;
}
public void setCellPhone(CellPhone cellPhone) {
this.cellPhone = cellPhone;
this.getCellPhone().setCellPhoneSystem(this);// 裝配(橋接,或者可以認(rèn)為對(duì)象類與其屬性類的傳遞)
}
public String getSystemName() {
return SystemName;
}
public void setSystemName(String systemName) {
SystemName = systemName;
}
}
3. 具體的維度屬性對(duì)象。
這里我們?cè)诓僮飨到y(tǒng)屬性和cpu屬性上各定義2個(gè)具體對(duì)象,
package test.design.bridge;
public class AndroidSystem extends CellPhoneSystem{
}
package test.design.bridge;
public class IOSSystem extends CellPhoneSystem{
}
package test.design.bridge;
/**
* 雙核cpu
* @author Administrator
*
*/
public class TwoCore extends CellPhoneCPU{
}
package test.design.bridge;
/**
* 四核cpu
* @author Administrator
*
*/
public class FourCore extends CellPhoneCPU{
}
4. 測(cè)試代碼。
其中說(shuō)了在需要擴(kuò)展維度的情況下,怎么擴(kuò)展的。
定義一個(gè)手機(jī)對(duì)象
package test.design.bridge;
public class Phone1 extends CellPhone{
//具體對(duì)象的屬性與邏輯
}
測(cè)試main函數(shù)
package test.design.bridge;
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
//任何一種具體的對(duì)象都是復(fù)雜多種屬性的集合,在此可以看出橋接模式在構(gòu)建對(duì)象時(shí)的靈活性
//產(chǎn)生一個(gè)具體對(duì)象1
CellPhone p1=new Phone1();
p1.setCellPhoneName(" IPhone 6 ");
CellPhoneSystem system1=new IOSSystem();//操作系統(tǒng)屬性維度
system1.setSystemName("ios7");
system1.setCellPhone(p1);//裝配
system1.systemWorks();//工作
/*裝配說(shuō)的簡(jiǎn)單點(diǎn)就是傳值。因?yàn)槲覀儼岩粋€(gè)對(duì)象的屬性按維度分開(kāi)來(lái)了,
那么橋接的時(shí)候就必須相互傳遞對(duì)象。即對(duì)象類可以調(diào)用子屬相類對(duì)象,
子屬性類對(duì)象也可以調(diào)用該對(duì)象類.
關(guān)于這樣的傳值方式有多種,你可以在構(gòu)造函數(shù)中傳遞,也可以在
調(diào)用具體邏輯方法時(shí)傳遞。這里我直接用set方法傳遞,只是為了更清楚.
如果某個(gè)屬性維度是必須出現(xiàn)的,那就可以在抽象類的構(gòu)造函數(shù)中傳入*/
CellPhoneCPU cpu1=new TwoCore();//cpu屬性維度
cpu1.setCpuName("A6");
cpu1.setCellPhone(p1);
cpu1.cpuWorks();
p1.works();//最終整體對(duì)象功能
/*
橋接模式就是為了應(yīng)對(duì)屬性的擴(kuò)展,在此說(shuō)的屬性必須是在維度確定的情況下。
比如,這里我們?cè)诙x手機(jī)對(duì)象時(shí),確定兩個(gè)屬性維度:操作系統(tǒng)和cpu型號(hào)。
以后再這兩個(gè)屬性中,需要擴(kuò)展時(shí),就可以使用該模式。比如,一種新的cpu
型號(hào)出現(xiàn)了,那么我不用重新設(shè)計(jì)現(xiàn)在的代碼,只要增添一個(gè)cpu類即可。
如果出現(xiàn)了新的維度屬性,比如手機(jī)對(duì)象必須考慮屏幕大小。那橋接模式
在此就需要從根本上修改代碼來(lái)了。
*/
System.out.println("-----------分割---------------------------");
//在cpu維度上擴(kuò)展。比如出現(xiàn)新型cpu:8核三星Exynos 5 Octa芯片".
//三星手機(jī)推出了GALAXY Note Ⅲ就是使用這種新型cpu. 寫一個(gè)新類EightCore擴(kuò)展cpu維度.
//同時(shí)定義這個(gè)手機(jī)對(duì)象GALAXY Note Ⅲ為PhoneGalaxyNote3
CellPhone note3=new PhoneGalaxyNote3();
note3.setCellPhoneName("GALAXY Note Ⅲ");
CellPhoneSystem system2=new AndroidSystem();
system2.setSystemName("android4");
system2.setCellPhone(note3);//裝配
system2.systemWorks();//工作
CellPhoneCPU cpu2=new EightCore();//最新8核cpu
cpu2.setCpuName("三星Exynos 5 Octa芯片");
cpu2.setCellPhone(note3);
cpu2.cpuWorks();
note3.works();//三星GALAXY Note Ⅲ新體驗(yàn)
}
}
如果需要擴(kuò)展,定義新的維度屬性
package test.design.bridge;
public class EightCore extends CellPhoneCPU {
}
package test.design.bridge;
public class PhoneGalaxyNote3 extends CellPhone{
//具體對(duì)象的屬性與邏輯
}
測(cè)試打印;
I am ios7 system. I am working for this cellphone: IPhone 6 I am cpu. My pattern is:A6 I am working for this cellphone: IPhone 6 --------------------- This cellphone is: IPhone 6 ,welcome to use. This cellphone detail infomation: 系統(tǒng)類型:ios7 cpu型號(hào):A6 --------------------- -----------分割--------------------------- I am android4 system. I am working for this cellphone:GALAXY Note Ⅲ I am cpu. My pattern is:三星Exynos 5 Octa芯片 I am working for this cellphone:GALAXY Note Ⅲ --------------------- This cellphone is:GALAXY Note Ⅲ,welcome to use. This cellphone detail infomation: 系統(tǒng)類型:android4 cpu型號(hào):三星Exynos 5 Octa芯片 ---------------------
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java動(dòng)態(tài)設(shè)置注解值及原理詳解
這篇文章主要介紹了Java動(dòng)態(tài)設(shè)置注解值及原理詳解,AnnotationInvocationHandler是注解的代理hander,通過(guò)反射獲取類的注解時(shí)會(huì)通過(guò)AnnotationInvocationHandler創(chuàng)建代理對(duì)象并將數(shù)據(jù)存儲(chǔ)到memberValues里,需要的朋友可以參考下2023-11-11
如何解決java:錯(cuò)誤:無(wú)效的源發(fā)行版:16
這篇文章主要介紹了如何解決java:錯(cuò)誤: 無(wú)效的源發(fā)行版:16問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
springboot整合通用Mapper簡(jiǎn)化單表操作詳解
這篇文章主要介紹了springboot整合通用Mapper簡(jiǎn)化單表操作,通用Mapper是一個(gè)基于Mybatis,將單表的增刪改查通過(guò)通用方法實(shí)現(xiàn),來(lái)減少SQL編寫的開(kāi)源框架,需要的朋友可以參考下2019-06-06
springboot打印接口調(diào)用日志的實(shí)例
這篇文章主要介紹了springboot打印接口調(diào)用日志的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Java虛擬機(jī)JVM之server模式與client模式的區(qū)別
這篇文章主要介紹了Java虛擬機(jī)JVM的client模式和Server模式兩者的區(qū)別和聯(lián)系2017-12-12
IntelliJ IDEA 2018 最新激活碼(截止到2018年1月30日)
這篇文章主要介紹了IntelliJ IDEA 2018 最新激活碼(截止到2018年1月30日)的相關(guān)資料,需要的朋友可以參考下2018-01-01
Java中ShardingSphere 數(shù)據(jù)分片的實(shí)現(xiàn)
其實(shí)很多人對(duì)分庫(kù)分表多少都有點(diǎn)恐懼,我們今天用ShardingSphere 給大家演示數(shù)據(jù)分片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09

