码迷,mamicode.com
首页 > 其他好文 > 详细

第二条 遇到多个构造器参数时,要考虑用构建器

时间:2014-04-29 15:31:47      阅读:371      评论:0      收藏:0      [点我收藏+]

标签:com   http   class   blog   style   div   img   code   java   size   javascript   

  静态工厂和构造器都有个共同的局限性,就是它们都不能够很好的扩展到大量的可选参数。

如:

mamicode.com,码迷
public class NutritionFacts{

     private final int servingSize;
     private final int servings;
     private final int calories;
     private final int fat;
     private final int sodium;
     private final int carbohydrate;

     public NutritionFacts(int servingSize, int servings){
           this(servingSize, servings, 0);
    }  

    public NutritionFacts(int srvingSize, int servings, int calories){
           this(servingSize, servings, calories, fat, 0);
    }

    public NutritionFacts(int servingSize, int servings, int calories,   int fat ){
           this(servingsSize, servings, calories, fat, 0);
    }
   
    public NutritionFacts(int servingSize, int servings, int calories, int fat, int sodium){
            this(servingsSize, servings, calories, fat, sodium,0);
    }

    public NutritionFacts(int servingSize, int servings, int calories,   int fat, int sodium, int carbohydrate){
            this.servingSize = servingSize;
            this.servings = servings;
            this.calories = caloriers;
            this.fat = fat;
            this.sodium = sodium;
            this.carbohydrate = carbohydrate;
    }

}
mamicode.com,码迷

我们可以看到利用构造器方式,有多个参数时,很难控制其灵活性。因而我们选择尝试给每个值设置默认值:

mamicode.com,码迷
public class NutritionFacts{
  private int servingSize = -1;
  private int servings = -1;
  private int calories = 0;
  private int fat = 0;
  private int sodium = 0;
  private int carbohydrate = 0;

  public NutritionFacts(){
     //   setters
     public void setServingSize(int val){
         servingSize = val;
    } 
    
     public void setServings(int val){
          servings = val;
     }
     //....
  }
}
mamicode.com,码迷

 

  调用初始化

mamicode.com,码迷
NutritionFacts cocaCola = new NutritionFacts();
cocaCola.setServingSize(240);
cocaCola.setServings(8);
cocaCola.setCalories(100);
cocaCola.setSodium(35);
cocaCola.setCarbohydrate(27);
mamicode.com,码迷

这是常见的javaBean模式,通过setter方法来构建对象,但是这也阻止了类做成不可变的可能,会有多线程安全问题。

 

那么我们来看看另外一种方式:

mamicode.com,码迷
public class NutritionFacts{

   private final int servingSize;
   private final int srvings;
   private final int calories;
   private final int fat;
   private final int sodium;
   private final int carbohydrate;

  // 静态内部类
public static class Builder{
// 必须元素
private final int servingSize; private final int servings; // 默认元素 private int calories = 0; private int fat = 0; private int carbohydrate = 0; private int sodium = 0; public Builder(int servingSize, int servings){ this.servingSize = servingSize; this.servings = servings; } public Builder calories(int val){ calories = val; return this; } public Builder carbohydrate(int val){ carbohydrate = val; return this; } public Builder sodium(int val){ sodium = val; return this; }
    // 在内部类中返回外部类对象的接口
public NutritionFacts build(){ return new NutritionsFacts(this); } } private NutritionFacts(Builder builder){ servingSize = builder.servingSize; servings = builder.servings; calories = builder.calories; fat = builder.fat; sodium = builder.sodium; carbodhydrate = builder.carbodhydrate; } }
mamicode.com,码迷

 

调用方式:

mamicode.com,码迷
NutritionFacts fact =  new NutritionFacts.builder(240,8).calories(100).sodium(46).carbohydrate(27).build();
mamicode.com,码迷

这样通过builder模式,实现了可选参数构建对象实例。

第二条 遇到多个构造器参数时,要考虑用构建器,码迷,mamicode.com

第二条 遇到多个构造器参数时,要考虑用构建器

标签:com   http   class   blog   style   div   img   code   java   size   javascript   

原文地址:http://www.cnblogs.com/goupb/p/3695525.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!