日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

java设计模式建造_Java设计模式——建造模式(Builder Pattern)

發(fā)布時(shí)間:2024/9/18 java 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java设计模式建造_Java设计模式——建造模式(Builder Pattern) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Builder模式常用例子

今天就簡單粗暴的介紹一下Builder模式概念和使用。

Dialog的創(chuàng)建

AlertDialog dialog = new AlertDialog.Builder(this)

.setIcon(R.mipmap.ic_launcher)

.setMessage("Hello World")

.setTitle("Dialog")

.create();

dialog.show();

OkHttp Request的創(chuàng)建

Request request = new Request.Builder()

.url("https://github.com/hongyangAndroid")

.build();

1

2

3

Builder模式的好處/優(yōu)點(diǎn)

客戶端代碼的可用性和可讀性得到了大大提高。與此同時(shí),構(gòu)造函數(shù)的參數(shù)數(shù)量明顯減少調(diào)用起來非常直觀。【不然創(chuàng)建dialog構(gòu)造函數(shù)時(shí)new AlertDialog(R.mipmap.ic_launcher,"Hello World","Dialog","How are you"),參數(shù)數(shù)量多的話客戶端感覺就是黑人問號(hào)】,在提高代碼可讀性的同時(shí),使用IDE提供的代碼補(bǔ)全功能也更加容易。

Builder例子

下面舉個(gè)關(guān)于麥當(dāng)勞點(diǎn)餐的栗子

public class McFood {

private int totalCount; //份數(shù)

private boolean addIce; //飲料是否去冰

private Hamburg hamburg; //漢堡包

private Drink drink; //飲品

private String remark; //備注

private boolean takeOut; //是否外帶

public static class Hamburg {//漢堡包類

public static final String HAMBURG_CHICKEN = "chicken";

public static final String HAMBURG_BEEF = "beef";

public Hamburg(String hamburgName) {

this.hamburgName = hamburgName;

}

public String getHamburgName() {

return hamburgName;

}

private String hamburgName;

}

public static class Drink {//飲品類

public static final String DRINK_COLA = "cola";

public static final String DRINK_SPRITE = "sprite";

public Drink(String drinkName) {

this.drinkName = drinkName;

}

private String drinkName;

public String getDrinkName() {

return drinkName;

}

}

}

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

引入Builder類,增強(qiáng)客戶端可用性和可讀性【McFood的內(nèi)部類】

public static class Builder {

private int totalCount = 0;

private boolean addIce = false;

private Hamburg hamburg = null;

private Drink drink = null;

private String remark = null;

private boolean takeOut = false;

public Builder totalCount(int totalCount) {

this.totalCount = totalCount;

return this;

}

public Builder addIce(boolean addIce) {

this.addIce = addIce;

return this;

}

public Builder hamburg(Hamburg hamburg) {

this.hamburg = hamburg;

return this;

}

public Builder drink(Drink drink) {

this.drink = drink;

return this;

}

public Builder remark(String remark) {

this.remark = remark;

return this;

}

public Builder takeOut(boolean takeOut) {

this.takeOut = takeOut;

return this;

}

public McFood create() { // 構(gòu)建,返回一個(gè)新對(duì)象

return new McFood(this);

}

}

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

當(dāng)然還要設(shè)置McFood的構(gòu)造方法【引入Builder】

public McFood(Builder builder) {

this.totalCount = builder.totalCount;

this.addIce = builder.addIce;

this.hamburg = builder.hamburg;

this.drink = builder.drink;

this.remark = builder.remark;

this.takeOut = builder.takeOut;

}

1

2

3

4

5

6

7

8

最后我們來看一下客戶端的操作方式

McFood food = new McFood.Builder()

.drink(new Drink(DRINK_COLA))

.addIce(false) //去冰

.hamburg(new Hamburg(HAMBURG_BEEF)) //牛肉漢堡

.takeOut(true) //外帶

.totalCount(3) //總共*3

.create();

1

2

3

4

5

6

7

看完代碼高下立判,調(diào)用傳統(tǒng)JAVA構(gòu)造器的客戶端代碼的可讀性遠(yuǎn)不如使用了Builder的客戶端代碼。同一類型的變量和空放置在一起被調(diào)用將會(huì)導(dǎo)致一些微妙的錯(cuò)誤。(試想,如果客戶端不小心顛倒了其中的幾個(gè)參數(shù)順序,編譯不會(huì)出錯(cuò)但在運(yùn)行時(shí)肯定出錯(cuò))【如果沒用builder模式,構(gòu)造方法把去冰/外帶 順序?qū)懛戳?#xff0c;那就GG了】

Builder模式的缺點(diǎn)

使用Builder模式是肯定會(huì)增加代碼量的。此外,盡管客戶端的代碼可讀性明顯改善,但隨之而來的客戶端代碼變得更加冗長。【但是前者更有價(jià)值】

Builder會(huì)增加個(gè)類代碼,這也意味著開發(fā)者在給類增加屬性時(shí)有時(shí)會(huì)忘記給該屬性添加支持的builder。為了克服這個(gè)問題,通常將builder嵌套到類中,這樣可以很容易地發(fā)現(xiàn)哪個(gè)相關(guān)的builder需要更新。盡管忘記的風(fēng)險(xiǎn)依舊存在,但是這風(fēng)險(xiǎn)就像忘記給類的新屬性增加 toString()、 equals(Object)、 hashCode()或其他類基于是所有屬性的方法一樣。

總結(jié)

構(gòu)建對(duì)象時(shí),如果碰到類有很多參數(shù)——其中很多參數(shù)類型相同而且很多參數(shù)可以為空時(shí),推薦Builder模式來完成。當(dāng)參數(shù)數(shù)量不多、類型不同而且都是必須出現(xiàn)時(shí),通過增加代碼實(shí)現(xiàn)Builder往往無法體現(xiàn)它的優(yōu)勢。在這種情況下,理想的方法是調(diào)用傳統(tǒng)的構(gòu)造函數(shù)。再者,如果不需要保持不變,那么就使用無參構(gòu)造函數(shù)調(diào)用相應(yīng)的set方法吧。

總結(jié)

以上是生活随笔為你收集整理的java设计模式建造_Java设计模式——建造模式(Builder Pattern)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。