第二章:创建和销毁对象。ITEM2:遇到多个构造器参数时要考虑用构建器。

如果一个类中有大量的可选参数,有以下几种方式:

1、重叠构造器:

package com.twoslow.cha2;

/**
 * 重叠构造器可行,但是当由许多参数的时候,客户端代码很难编写。
 * @author sai
 *
 */
public class Item201 {

    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 Item201(int servingSize, int servings) {
        this(servingSize, servings, 0);
    }

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

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

}

2、javaBeans模式,提供setter方法:

package com.twoslow.cha2;

/**
 * JavaBeans模式,调用setter方法设置每个必要的参数。
 * 构造过程被分到了几个调用中,在构造过程中JavaBean可能处于不一致的状态。
 * @author sai
 *
 */
public class Item202 {

    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 Item202() {
    }

    public void setServingSize(int servingSize) {
        this.servingSize = servingSize;
    }

    public void setServings(int servings) {
        this.servings = servings;
    }

    public void setCalories(int calories) {
        this.calories = calories;
    }

    public void setFat(int fat) {
        this.fat = fat;
    }

    public void setSodium(int sodium) {
        this.sodium = sodium;
    }

    public void setCarbohydrate(int carbohydrate) {
        this.carbohydrate = carbohydrate;
    }

}

3、构建器:

package com.twoslow.cha2;

/**
 * 构建器,不足:1、为了创建对象,必须显创建它的构建器。
 * 2、比重叠构造器更加冗长,只在有很多参数的时候才使用,>=4.
 * @author sai
 *
 */
public class Item203 {

    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 {//可以通过实现泛型接口构建任何类型的Builder
        private final int servingSize ; 
        private final int servings ; 
        
        private int calories = 0; 
        private int fat  = 0; 
        private int sodium  = 0; 
        private int carbohydrate  = 0;
        
        public Builder(int servingSize , int servings) {
            this.servingSize = servingSize ; 
            this.servings = servings ; 
        }
        
        public Builder calories(int calories) {
            this.calories = calories ; 
            return this ; 
        }
        public Builder fat(int fat) {
            this.fat = fat ; 
            return this ; 
        }
        public Builder sodium(int sodium) {
            this.sodium = sodium ; 
            return this ; 
        }
        public Builder carbohydrate(int carbohydrate) {
            this.carbohydrate = carbohydrate ; 
            return this ; 
        }
        
        public Item203 build() {
            return new Item203(this) ;
        }
    }
    
        public Item203(Builder builder) {
            this.servingSize = builder.servingSize ; 
            this.servings = builder.servings ; 
            this.calories = builder.calories ; 
            this.fat = builder.fat ; 
            this.sodium = builder.sodium ; 
            this.carbohydrate = builder.carbohydrate ; 
        }

}

 

转载于:https://my.oschina.net/u/726229/blog/310077

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值