当前位置:七道奇文章资讯编程技术Java编程
日期:2012-04-05 13:24:00  来源:本站整理

Java学习笔记-02碰到多个构造器参数时要考虑用构建器[Java编程]

赞助商链接



  本文“Java学习笔记-02碰到多个构造器参数时要考虑用构建器[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

  静态工厂和构造器有个共同的范围性:它们都不能很好地扩大到大量的可选参数.考虑用一个类表示包装食物表面显示的营养成份标签.这些标签中有几个域是必须的:每份的含量、每罐的含量以及每份的卡路里,还有超越20哥可选域:总脂肪量、饱和脂肪量、转化脂肪、胆固醇、钠等等.大大都产品在某几个可选域中城市有非零的值.

  关于这样的类,应当用哪类构造器大概静态办法来编写呢?

  1. 重叠构造器情势

  [java]

  // Telescoping constructor pattern - does not scale well!

  public class NutritionFacts {

  private final int servingSize; // (mL) required

  private final int servings; // (per container) required

  private final int calories; // optional

  private final int fat; // (g) optional

  private final int sodium; // (mg) optional

  private final int carbohydrate; // (g) optional

  public NutritionFacts(int servingSize, int servings) {

  this(servingSize, servings, 0);

  }

  public NutritionFacts(int servingSize, int servings,

  int calories) {

  this(servingSize, servings, calories, 0);

  }

  public NutritionFacts(int servingSize, int servings,

  int calories, int fat) {

  this(servingSize, servings, calories, fat, 0);

  }

  public NutritionFacts(int servingSize, int servings,

  int calories, int fat, int sodium) {

  this(servingSize, 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 = calories;

  this.fat = fat;

  this.sodium = sodium;

  this.carbohydrate = carbohydrate;

  }

  public static void main(String[] args) {

  NutritionFacts cocaCola =

  new NutritionFacts(240, 8, 100, 0, 35, 27);

  }

  }

  // Telescoping constructor pattern - does not scale well!

  public class NutritionFacts {

  private final int servingSize; // (mL) required

  private final int servings; // (per container) required

  private final int calories; // optional

  private final int fat; // (g) optional

  private final int sodium; // (mg) optional

  private final int carbohydrate; // (g) optional

  public NutritionFacts(int servingSize, int servings) {

  this(servingSize, servings, 0);

  }

  public NutritionFacts(int servingSize, int servings,

  int calories) {

  this(servingSize, servings, calories, 0);

  }

  public NutritionFacts(int servingSize, int servings,

  int calories, int fat) {

  this(servingSize, servings, calories, fat, 0);

  }

  public NutritionFacts(int servingSize, int servings,

  int calories, int fat, int sodium) {

  this(servingSize, 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 = calories;

  this.fat = fat;

  this.sodium = sodium;

  this.carbohydrate = carbohydrate;

  }

  public static void main(String[] args) {

  NutritionFacts cocaCola =

  new NutritionFacts(240, 8, 100, 0, 35, 27);

  }

  }2. JavaBeans情势

  [java]

  // Telescoping constructor pattern - does not scale well!

  public class NutritionFacts {

  private final int servingSize; // (mL) required

  private final int servings; // (per container) required

  private final int calories; // optional

  private final int fat; // (g) optional

  private final int sodium; // (mg) optional

  private final int carbohydrate; // (g) optional

  public NutritionFacts(int servingSize, int servings) {

  this(servingSize, servings, 0);

  }

  public NutritionFacts(int servingSize, int servings,

  int calories) {

  this(servingSize, servings, calories, 0);

  }

  public NutritionFacts(int servingSize, int servings,

  int calories, int fat) {

  this(servingSize, servings, calories, fat, 0);

  }

  public NutritionFacts(int servingSize, int servings,

  int calories, int fat, int sodium) {

  this(servingSize, 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 = calories;

  this.fat = fat;

  this.sodium = sodium;

  this.carbohydrate = carbohydrate;

  }

  public static void main(String[] args) {

  NutritionFacts cocaCola =

  new NutritionFacts(240, 8, 100, 0, 35, 27);

  }

  }

  // Telescoping constructor pattern - does not scale well!

  public class NutritionFacts {

  private final int servingSize; // (mL) required

  private final int servings; // (per container) required

  private final int calories; // optional

  private final int fat; // (g) optional

  private final int sodium; // (mg) optional

  private final int carbohydrate; // (g) optional

  public NutritionFacts(int servingSize, int servings) {

  this(servingSize, servings, 0);

  }

  public NutritionFacts(int servingSize, int servings,

  int calories) {

  this(servingSize, servings, calories, 0);

  }

  public NutritionFacts(int servingSize, int servings,

  int calories, int fat) {

  this(servingSize, servings, calories, fat, 0);

  }

  public NutritionFacts(int servingSize, int servings,

  int calories, int fat, int sodium) {

  this(servingSize, 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 = calories;

  this.fat = fat;

  this.sodium = sodium;

  this.carbohydrate = carbohydrate;

  }

  public static void main(String[] args) {

  NutritionFacts cocaCola =

  new NutritionFacts(240, 8, 100, 0, 35, 27);

  }

  }3. Builder情势,既能保证安全性,又能保证可读性.

  [java]

  // Builder Pattern

  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 static class Builder {

  // Required parameters

  private final int servingSize;

  private final int servings;

  // Optional parameters - initialized to default values

  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 fat(int val)

  { fat = 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 NutritionFacts(this);

  }

  }

  private NutritionFacts(Builder builder) {

  servingSize = builder.servingSize;

  servings = builder.servings;

  calories = builder.calories;

  fat = builder.fat;

  sodium = builder.sodium;

  carbohydrate = builder.carbohydrate;

  }

  public static void main(String[] args) {

  NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).

  calories(100).sodium(35).carbohydrate(27).build();

  }

  }

  // Builder Pattern

  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 static class Builder {

  // Required parameters

  private final int servingSize;

  private final int servings;

  // Optional parameters - initialized to default values

  private int calories = 0;


  以上是“Java学习笔记-02碰到多个构造器参数时要考虑用构建器[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 利用Javascript实现网页水印(非图片水印)
  • Java开辟环境的搭建
  • Ubuntu java安装与配置
  • 办理Ubuntu 10.04 Firefox3.6 Java浏览器插件不工作的问
  • Ubuntu重装后Java环境的设置
  • Sun Java进入Ubuntu 10.10软件中央
  • Ubuntu 10.10配置Java开辟环境
  • 在Ubuntu 10.10中配置Java环境变量的办法
  • Ubuntu下Java环境的搭建
  • Ubuntu 10.04 下安装 Java, JRE
  • Ubuntu 10.04下的搭建SUN JAVA开辟环境
  • Ubuntu 12.04安装java7
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2021 www.xiamiku.com. All Rights Reserved .