本文實(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ò)展。沒辦法維護(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ò)展。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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)鍵看你的具體功能)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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ì)象,
1
2
3
|
package test.design.bridge; public class androidsystem extends cellphonesystem{ } |
1
2
3
|
package test.design.bridge; public class iossystem extends cellphonesystem{ } |
1
2
3
4
5
6
7
8
|
package test.design.bridge; /** * 雙核cpu * @author administrator * */ public class twocore extends cellphonecpu{ } |
1
2
3
4
5
6
7
8
|
package test.design.bridge; /** * 四核cpu * @author administrator * */ public class fourcore extends cellphonecpu{ } |
4. 測(cè)試代碼。
其中說(shuō)了在需要擴(kuò)展維度的情況下,怎么擴(kuò)展的。
定義一個(gè)手機(jī)對(duì)象
1
2
3
4
|
package test.design.bridge; public class phone1 extends cellphone{ //具體對(duì)象的屬性與邏輯 } |
測(cè)試main函數(shù)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
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ì)象的屬性按維度分開來(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ò)展,定義新的維度屬性
1
2
3
|
package test.design.bridge; public class eightcore extends cellphonecpu { } |
1
2
3
4
|
package test.design.bridge; public class phonegalaxynote3 extends cellphone{ //具體對(duì)象的屬性與邏輯 } |
測(cè)試打印;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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芯片 --------------------- |
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
原文鏈接:http://blog.csdn.net/lushuaiyin/article/details/9345495