本文實例講述了java設計模式之橋接模式。分享給大家供大家參考,具體如下:
概念:
橋接模式(bridge pattern):將抽象部分與它的實現部分分離,使它們都可以獨立地變化。
橋接模式將繼承關系轉換為關聯關系,從而降低了類與類之間的耦合,減少了代碼編寫量。
什么情況下會用橋接模式?
簡單的說就是我們在抽象對象的特征時,對象的特征屬性又很抽象,不得不把屬性再次抽象。
否則的話,具體子類的數量將會成幾何增長,而且不易擴展。沒辦法維護現有代碼。
舉例,我們在抽象手機這二個對象時,它的幾個屬性,如操作系統,cpu,屏幕,運營商網絡等都很復雜。我們不能簡單的把這幾個屬性直接定義,必須再次抽象化。而具體的一個手機對象就是這些屬性的組合,但不是簡單的組合,屬性需要實現自己作為屬性的功能。在這樣的設計下,代碼的維護和擴展也就容易了。
注意:在說這個模式的時候,我不能保證說的和寫得例子都是正確的,畢竟我也是新接觸到,所有例子均基于與個人理解。
我認為的橋接模式說明圖:
下面是例子:
1. 首先定義抽象類,抽象和描述對象的特征。
在對象的屬性上劃分維度,為了以后橋接和擴展。
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( "系統類型:" + this .getcellphonesystem().getsystemname()); system.out.println( "cpu型號:" + 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. 屬性維度的抽象。(可以使用接口定義,關鍵看你的具體功能)
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被抽象成一個維度,為了以后擴展 * @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 ); // 裝配(橋接,或者可以認為對象類與其屬性類的傳遞) } 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; /** * 屬性操作系統被抽象成一個維度,為了以后擴展 * @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 ); // 裝配(橋接,或者可以認為對象類與其屬性類的傳遞) } public string getsystemname() { return systemname; } public void setsystemname(string systemname) { systemname = systemname; } } |
3. 具體的維度屬性對象。
這里我們在操作系統屬性和cpu屬性上各定義2個具體對象,
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. 測試代碼。
其中說了在需要擴展維度的情況下,怎么擴展的。
定義一個手機對象
1
2
3
4
|
package test.design.bridge; public class phone1 extends cellphone{ //具體對象的屬性與邏輯 } |
測試main函數
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) { //任何一種具體的對象都是復雜多種屬性的集合,在此可以看出橋接模式在構建對象時的靈活性 //產生一個具體對象1 cellphone p1= new phone1(); p1.setcellphonename( " iphone 6 " ); cellphonesystem system1= new iossystem(); //操作系統屬性維度 system1.setsystemname( "ios7" ); system1.setcellphone(p1); //裝配 system1.systemworks(); //工作 /*裝配說的簡單點就是傳值。因為我們把一個對象的屬性按維度分開來了, 那么橋接的時候就必須相互傳遞對象。即對象類可以調用子屬相類對象, 子屬性類對象也可以調用該對象類. 關于這樣的傳值方式有多種,你可以在構造函數中傳遞,也可以在 調用具體邏輯方法時傳遞。這里我直接用set方法傳遞,只是為了更清楚. 如果某個屬性維度是必須出現的,那就可以在抽象類的構造函數中傳入*/ cellphonecpu cpu1=new twocore();//cpu屬性維度 cpu1.setcpuname("a6"); cpu1.setcellphone(p1); cpu1.cpuworks(); p1.works();//最終整體對象功能 /* 橋接模式就是為了應對屬性的擴展,在此說的屬性必須是在維度確定的情況下。 比如,這里我們在定義手機對象時,確定兩個屬性維度:操作系統和cpu型號。 以后再這兩個屬性中,需要擴展時,就可以使用該模式。比如,一種新的cpu 型號出現了,那么我不用重新設計現在的代碼,只要增添一個cpu類即可。 如果出現了新的維度屬性,比如手機對象必須考慮屏幕大小。那橋接模式 在此就需要從根本上修改代碼來了。 */ system.out.println( "-----------分割---------------------------" ); //在cpu維度上擴展。比如出現新型cpu:8核三星exynos 5 octa芯片". //三星手機推出了galaxy note ⅲ就是使用這種新型cpu. 寫一個新類eightcore擴展cpu維度. //同時定義這個手機對象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 ⅲ新體驗 } } |
如果需要擴展,定義新的維度屬性
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{ //具體對象的屬性與邏輯 } |
測試打印;
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: 系統類型:ios7 cpu型號: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: 系統類型:android4 cpu型號:三星exynos 5 octa芯片 --------------------- |
希望本文所述對大家java程序設計有所幫助。
原文鏈接:http://blog.csdn.net/lushuaiyin/article/details/9345495