安全黑客
当前位置:安全黑客文章资讯编程技术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编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看安全黑客为您推荐以下文章:
  • 甲骨文的Java序器安全管理器旁路远程执行代码漏洞
  • 甲骨文的Java ManagedObjectManagerFactory安全管理器旁路远程代码执行Vulnerabillity
  • 甲骨文的Java AWT内存损坏远程代码执行漏洞
  • 甲骨文的Java CMMImageLayout内存腐败远程代码执行漏洞
  • 甲骨文的Java ByteComponentRaster缓冲区溢出远程执行代码漏洞
  • 甲骨文的Java AWT内存损坏远程代码执行漏洞
  • 甲骨文的Java IntegerComponentRaster缓冲区溢出远程执行代码漏洞
  • 甲骨文的Java ShortComponentRaster缓冲区溢出远程执行代码漏洞
  • Oracle的Java cmmColorConvert的远程代码执行漏洞
  • 甲骨文的Java运行时环境AWT mediaLib的远程代码执行漏洞
  • 甲骨文Java图像ColorConvert远程代码执行漏洞
  • 甲骨文Java密钥库安全管理器旁路远程代码执行漏洞
  • 本文地址: 与您的QQ/BBS好友分享!

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

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    免责条款 - 广告合作 - 下载声明 - 欢迎投稿 - 友情连接 - 网站地图 -
    Copyright © 2012-2013 www.110hack.com. All Rights Reserved .