<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    一葉笑天
    雄關(guān)漫道真如鐵, 而今邁步從頭越。 從頭越, 蒼山如海, 殘陽如血。
    posts - 73,comments - 7,trackbacks - 0
    靜態(tài)工廠方法和public構(gòu)造函數(shù)有一個共同的弊病:在大量的可選參數(shù)面前,他們不能很好的體現(xiàn)可伸展性。可以看一個例子如下:
     1// Telescoping constructor pattern - does not scale well!
     2public class NutritionFacts {
     3private final int servingSize; // (mL) required
     4private final int servings; // (per container) required
     5private final int calories; // optional
     6private final int fat; // (g) optional
     7private final int sodium; // (mg) optional
     8private final int carbohydrate; // (g) optional
     9public NutritionFacts(int servingSize, int servings) {
    10this(servingSize, servings, 0);
    11}

    12public NutritionFacts(int servingSize, int servings,
    13int calories) {
    14this(servingSize, servings, calories, 0);
    15}

    16public NutritionFacts(int servingSize, int servings,
    17int calories, int fat) {
    18this(servingSize, servings, calories, fat, 0);
    19}

    20public NutritionFacts(int servingSize, int servings,
    21int calories, int fat, int sodium) {
    22this(servingSize, servings, calories, fat, sodium, 0);
    23}

    24public NutritionFacts(int servingSize, int servings,
    25int calories, int fat, int sodium, int carbohydrate) {
    26this.servingSize = servingSize;
    27this.servings = servings;
    28this.calories = calories;
    29this.fat = fat;
    30this.sodium = sodium;
    31this.carbohydrate = carbohydrate;
    32}

    33}

    34

    當(dāng)我們需要創(chuàng)建實例的時候,需要知道而且傳遞每個參數(shù)的值到構(gòu)造函數(shù)中。例如:
    1NutritionFacts cocaCola =
    2new NutritionFacts(240810003527);NutritionFacts cocaCola =
    3new NutritionFacts(240810003527);

    解決這個問題的方法可以使用JavaBeans pattern:此方式中你可以調(diào)用無參數(shù)的構(gòu)造函數(shù)來創(chuàng)建對象,然后調(diào)用setter方法來set每個需要去的參數(shù)和可選的參數(shù)項。 例如
     1// JavaBeans Pattern - allows inconsistency, mandates mutability
     2public class NutritionFacts {
     3// Parameters initialized to default values (if any)
     4private int servingSize = -1// Required; no default value
     5private int servings = -1// " " " "
     6private int calories = 0;
     7private int fat = 0;
     8private int sodium = 0;
     9private int carbohydrate = 0;
    10public NutritionFacts() { }
    11// Setters
    12public void setServingSize(int val) { servingSize = val; }
    13public void setServings(int val) { servings = val; }
    14public void setCalories(int val) { calories = val; }
    15public void setFat(int val) { fat = val; }
    16public void setSodium(int val) { sodium = val; }
    17public void setCarbohydrate(int val) { carbohydrate = val; }
    18}

    19

    這樣可以很容易的閱讀和很容易的調(diào)用。
    1 NutritionFacts cocaCola = new NutritionFacts();
    2 cocaCola.setServingSize(240);
    3 cocaCola.setServings(8);
    4 cocaCola.setCalories(100);
    5 cocaCola.setSodium(35);
    6 cocaCola.setCarbohydrate(27);

    但是,JavaBeans模式也有嚴(yán)重的問題存在。因為構(gòu)造函數(shù)被分割成多個調(diào)用,JavaBean可能在和構(gòu)造函數(shù)不能保持一致性。JavaBean阻止了讓類成為immutable的可能。
    可以結(jié)合使用構(gòu)造函數(shù)模式的安全性和JavaBeans模式的易讀性。它就是Builder模式。
    1.客戶端使用所有必須參數(shù)去調(diào)用構(gòu)造函數(shù)來得到一個builder對象。
    2.客戶端在builder上調(diào)用類似于setter的方法設(shè)置每個可選項參數(shù)。
    3.客戶端調(diào)用無參數(shù)的build方法來生成對象,這個對象是immutable的。
    注意builder是要構(gòu)造類的靜態(tài)成員類。如下例所示:
     1 // Builder Pattern
     2 public class NutritionFacts {
     3 private final int servingSize;
     4 private final int servings;
     5 private final int calories;
     6 private final int fat;
     7 private final int sodium;
     8 private final int carbohydrate;
     9 public static class Builder {
    10 // Required parameters
    11 private final int servingSize;
    12 private final int servings;
    13 // Optional parameters - initialized to default values
    14 private int calories = 0;
    15 private int fat = 0;
    16 private int carbohydrate = 0;
    17 private int sodium = 0;
    18 public Builder(int servingSize, int servings) {
    19 this.servingSize = servingSize;
    20 this.servings = servings;
    21 }
    22 public Builder calories(int val)
    23 { calories = val; return this; }
    24 public Builder fat(int val)
    25 { fat = val; return this; }
    26 public Builder carbohydrate(int val)
    27 { carbohydrate = val; return this; }
    28 public Builder sodium(int val)
    29 { sodium = val; return this; }
    30 public NutritionFacts build() {
    31 return new NutritionFacts(this);
    32 }
    33 }
    34 private NutritionFacts(Builder builder) {
    35 servingSize = builder.servingSize;
    36 servings = builder.servings;
    37 calories = builder.calories;
    38 fat = builder.fat;
    39 sodium = builder.sodium;
    40 carbohydrate = builder.carbohydrate;
    41 }
    42 }
    43 

    注意NutritionFacts是immutable, 所有參數(shù)的缺省值是在單一位置。builder的setter方法返回builder自身,因此調(diào)用者可以被鏈接到。因此客戶端代碼如下:
    1NutritionFacts cocaCola = new NutritionFacts.Builder(2408)
    2calories(100).sodium(35).carbohydrate(27).build();
    Builder模式模仿了Ada和Python的可選參數(shù)命名法則。
    Builder模式超越構(gòu)造函數(shù)的一點優(yōu)勢是builder能夠擁有多個變參。構(gòu)造函數(shù)類似于函數(shù)只能有一個變參。
    總而言之,客戶端傳遞builder給方法能使方法在客戶端創(chuàng)建一個或者多個對象。為了使用這種用法,需要用一種類型表示builder。如下所示:
    1// A builder for objects of type T
    2public interface Builder<T> {
    3public T build();
    4}
    NutritionFacts.Builder聲明為實現(xiàn)Builder<NutritionFacts>.
    Builder模式也有自己的缺點。為了創(chuàng)建一個對象,首先需要創(chuàng)建它的builder。然而創(chuàng)建builder的成本不大可能在實踐中被注意到。但是在一些嚴(yán)格要求性能的情況下也會存在問題。
    總結(jié):
    當(dāng)構(gòu)造函數(shù)或者靜態(tài)工廠要處理大量參數(shù),尤其其中很多參數(shù)是可選的時候,Builder模式是個不錯的選擇。
    Client代碼將會易讀寫,而且比JavaBeans更安全。

    In summary, the Builder pattern is a good choice when designing classes
    whose constructors or static factories would have more than a handful of
    parameters, especially if most of those parameters are optional. Client code is
    much easier to read and write with builders than with the traditional telescoping
    constructor pattern, and builders are much safer than JavaBeans.



    posted on 2008-06-17 21:47 一葉笑天 閱讀(314) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 免费大黄网站在线观| 亚洲 日韩 色 图网站| 亚洲精品亚洲人成在线麻豆| 亚洲人成网站在线播放2019| 日韩精品人妻系列无码专区免费| 拔擦拔擦8x华人免费久久| 亚洲日本在线免费观看| 99久久久国产精品免费牛牛四川| 色在线亚洲视频www| 国产妇乱子伦视频免费| 国产A在亚洲线播放| 99久久成人国产精品免费| 久久久久亚洲爆乳少妇无| 老牛精品亚洲成av人片| 最近中文字幕无吗高清免费视频| 亚洲精品高清国产麻豆专区| 免费欧洲毛片A级视频无风险| 久久亚洲中文无码咪咪爱| 日韩中文无码有码免费视频| 久艹视频在线免费观看| 亚洲精品无码专区久久久| 国产色爽免费无码视频| 亚洲AV一宅男色影视| 18级成人毛片免费观看| 亚洲免费观看网站| 大学生a级毛片免费观看| 国产av无码专区亚洲av毛片搜 | 免费人成在线观看播放a| 日本免费人成黄页在线观看视频| 久久精品国产免费一区| 亚洲福利一区二区| 亚洲中文字幕无码永久在线| 国产成人精品无码免费看| 国产精品亚洲一区二区三区久久 | 国产精品自拍亚洲| 亚洲成人黄色网址| 久久夜色精品国产嚕嚕亚洲av| 国色精品va在线观看免费视频| 精品亚洲国产成人av| 亚洲AV成人影视在线观看| 亚洲小说区图片区|