激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Java設(shè)計(jì)模式之橋接模式實(shí)例詳解

Java設(shè)計(jì)模式之橋接模式實(shí)例詳解

2021-01-08 11:55索隆 Java教程

這篇文章主要介紹了Java設(shè)計(jì)模式之橋接模式,結(jié)合實(shí)例形式詳細(xì)分析了橋接模式的概念、功能、Java實(shí)現(xiàn)方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下

本文實(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ō)明圖:

Java設(shè)計(jì)模式之橋接模式實(shí)例詳解

下面是例子:

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产91片 | 久久精品国产清自在天天线 | 久久久久久久久久久久久九 | 欧美精品v国产精品v日韩精品 | 精品一区二区三区毛片 | 国产成人精品一区二区仙踪林 | 欧美a在线观看 | 国产亚洲精品久久久久久久久久 | 91精品国产乱码久 | 久久精品视频亚洲 | 欧美精品一区二区性色 | 91麻豆精品国产91久久久更新资源速度超快 | 91福利国产在线观一区二区 | 久久蜜桃香蕉精品一区二区三区 | 久久久久久久久久久久久九 | 亚洲视频在线免费看 | 中文字幕在线网站 | 欧美91看片特黄aaaa | 一级国产航空美女毛片内谢 | 精品久久www | 91精品国产乱码久久久久久久久 | 毛片在线免费视频 | 成年免费观看视频 | 欧美成人高清视频 | 视频一区 日韩 | 91av大片| 在线播放免费视频 | 精品一区二区视频在线观看 | 欧美成人一级片 | 国产美女的小嫩bbb图片 | 黄色免费不卡视频 | 日本欧美在线播放 | 色播视频在线播放 | 黄网站在线免费 | 欧美成人免费一区二区三区 | 一级免费黄色免费片 | 中文字幕亚洲一区二区三区 | 男男啪羞羞视频网站 | 黄色二区三区 | 国产一区二区久久精品 | 国产精品性夜天天视频 |