本文實例講述了java使用橋接模式實現(xiàn)開關(guān)和電燈照明功能。分享給大家供大家參考,具體如下:
一、模式定義
橋接模式,也稱橋梁模式,在軟件系統(tǒng)中,由于自身的邏輯,具有兩個或多個維度的變化,如何應(yīng)對這種多維度的變化,橋接模式使得軟件系統(tǒng)能夠輕松地沿著多個方向進(jìn)行變化,而又不引入額外的復(fù)雜度。
橋接模式三個關(guān)鍵詞為:抽象化,實現(xiàn)化,脫耦
二、模式舉例
1 橋接模式分析方法
我們借用電燈照明來說明該模式。
不使用繼承,使用對象組合的方式,將開關(guān)和電燈的強關(guān)聯(lián)關(guān)系變成弱關(guān)聯(lián)關(guān)系。
2 橋接模式靜態(tài)類模型
3 代碼示例
3.1 創(chuàng)建電燈接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.demo.bridge.lights; /** * 電燈接口 * * @author * */ public interface ilight { // 接通電流 public void electricconnected(); // 照明 public void light(); // 電流關(guān)閉 public void electricclosed(); } |
3.2 創(chuàng)建一般開關(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 com.demo.bridge.switchs; import com.demo.bridge.lights.ilight; /** * 開關(guān)頂層類 * * @author * */ public class baseswitch { // 使用組合 設(shè)置ilight為內(nèi)部私有屬性 此為橋梁 protected ilight light; // 構(gòu)造方法將 外部的light類型注入進(jìn)來 public baseswitch(ilight light) { this .light = light; } /** * 開燈方法 */ public final void makelight() { // 打開開關(guān) 接通電流 this .light.electricconnected(); // 照明 this .light.light(); // 關(guān)閉開關(guān) 關(guān)閉電流 this .light.electricclosed(); } } |
3.3 創(chuàng)建遙控開關(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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package com.demo.bridge.switchs.sub; import com.demo.bridge.lights.ilight; import com.demo.bridge.switchs.baseswitch; /** * 遙控開關(guān) 繼承baseswitch 擴展功能 * * @author * */ public class remotecontrolswitch extends baseswitch { // 構(gòu)造方法 public remotecontrolswitch(ilight light) { super (light); } /** * 使用遙控開關(guān)控制開燈 * * @param opercolor * 燈顏色 */ public final void makeremotelight( int opercolor) { // 打開開關(guān) 接通電流 this .light.electricconnected(); // 照明 this .light.light(); string color = "" ; switch (opercolor) { case 1 : color = "暖色" ; break ; case 2 : color = "藍(lán)色" ; break ; case 3 : color = "紅色" ; break ; default : color = "白色" ; break ; } system.out.println( " ...現(xiàn)在是" + color + "!" ); // 關(guān)閉開關(guān) 關(guān)閉電流 this .light.electricclosed(); } } |
3.4 白熾燈實現(xià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
|
package com.demo.bridge.lights.impl; import com.demo.bridge.lights.ilight; /** * 白熾燈 實現(xiàn) * * @author * */ public class incandescentlight implements ilight { // 電流關(guān)閉 public void electricclosed() { system.out.println( "白熾燈被關(guān)閉了..." ); } // 接通電流 public void electricconnected() { system.out.println( "白熾燈被打開了..." ); } // 照明 public void light() { system.out.println( "白熾燈照明!" ); } } |
3.5 水晶燈實現(xià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
|
package com.demo.bridge.lights.impl; import com.demo.bridge.lights.ilight; /** * 水晶燈 實現(xiàn) * * @author * */ public class crystallight implements ilight { // 電流關(guān)閉 public void electricclosed() { system.out.println( "水晶燈被關(guān)閉了..." ); } // 接通電流 public void electricconnected() { system.out.println( "水晶燈被打開了..." ); } // 照明 public void light() { system.out.println( "水晶燈照明!" ); } } |
3.6 一般開關(guān)控制白熾燈,遙控開關(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
28
29
30
31
32
|
package com.demo; import com.demo.bridge.lights.ilight; import com.demo.bridge.lights.impl.crystallight; import com.demo.bridge.lights.impl.incandescentlight; import com.demo.bridge.switchs.baseswitch; import com.demo.bridge.switchs.sub.remotecontrolswitch; /** * 客戶端應(yīng)用程序 * * @author * */ public class clientforbridge { /** * @param args */ public static void main(string[] args) { // 白熾燈 實例 ilight incandescentlight = new incandescentlight(); // 水晶燈 實例 ilight crystallight = new crystallight(); // 一般開關(guān) system.out.println( "-- 一般開關(guān) -- " ); baseswitch switch1 = new baseswitch(incandescentlight); switch1.makelight(); system.out.println( "\n-- 遙控開關(guān) -- " ); // 遙控開關(guān) remotecontrolswitch remotecontrolswitch = new remotecontrolswitch( crystallight); remotecontrolswitch.makeremotelight( 1 ); } } |
運行結(jié)果:
-- 一般開關(guān) --
白熾燈被打開了...
白熾燈照明!
白熾燈被關(guān)閉了...
-- 遙控開關(guān) --
水晶燈被打開了...
水晶燈照明!
...現(xiàn)在是暖色!
水晶燈被關(guān)閉了...
3.7 一般開關(guān)控制水晶燈,遙控開關(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
28
29
30
31
32
|
package com.demo; import com.demo.bridge.lights.ilight; import com.demo.bridge.lights.impl.crystallight; import com.demo.bridge.lights.impl.incandescentlight; import com.demo.bridge.switchs.baseswitch; import com.demo.bridge.switchs.sub.remotecontrolswitch; /** * 客戶端應(yīng)用程序 * * @author * */ public class clientforbridge { /** * @param args */ public static void main(string[] args) { // 白熾燈 實例 ilight incandescentlight = new incandescentlight(); // 水晶燈 實例 ilight crystallight = new crystallight(); // 一般開關(guān) system.out.println( "-- 一般開關(guān) -- " ); baseswitch switch1 = new baseswitch(crystallight); switch1.makelight(); system.out.println( "\n-- 遙控開關(guān) -- " ); // 遙控開關(guān) remotecontrolswitch remotecontrolswitch = new remotecontrolswitch( incandescentlight); remotecontrolswitch.makeremotelight( 1 ); } } |
運行結(jié)果
-- 一般開關(guān) --
水晶燈被打開了...
水晶燈照明!
水晶燈被關(guān)閉了...
-- 遙控開關(guān) --
白熾燈被打開了...
白熾燈照明!
...現(xiàn)在是暖色!
白熾燈被關(guān)閉了...
三、設(shè)計原則
1 盡量使用對象聚合弱關(guān)聯(lián),避免使用繼承強關(guān)聯(lián)。
2 抽象化和實現(xiàn)化脫耦。
四、使用場合
1 不希望在抽象類和實現(xiàn)部分之間有一個固定的綁定關(guān)系
2 類的抽象及實現(xiàn)部分都應(yīng)該可以通過孑類的方法加以擴充
3 對一個抽象的實現(xiàn)部分的修改對客戶不產(chǎn)生影響,即客戶代碼不必重新編譯
五、橋接模式靜態(tài)類圖
希望本文所述對大家java程序設(shè)計有所幫助。
原文鏈接:https://blog.csdn.net/chengqiuming/article/details/70140539