zoukankan      html  css  js  c++  java
  • Effective Java 02 Consider a builder when faced with many constructor parameters

    Advantage

    1. It simulates named optional parameters which is easily used to client API.
    2. Detect the invariant failure(validation error of field) as soon as the invalid parameters are passed, instead of waiting for build to be invoked.
    3. The builder can fill in some fields automatically such as a serial number.
    4. Avoid Class.newInstancebreaks compile-time exception checking

    Disadvantage

    1. A builder must be created first. It's time cost and verbose. It would be better to be used when there are more than 4 parameters.    

    Demo

    package com.effectivejava.creatingobject;

    // 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;

    }

       

    /*

    * (non-Javadoc)

    *

    * @see java.lang.Object#toString()

    */

    @Override

    public String toString() {

    // TODO Auto-generated method stub

    return String

    .format("NutritionFacts[servingSize:%d, servings:%d, calories: %d, fat: %d, sodium: %d, carbohydrate: %d]",

    this.servingSize, this.servings, this.calories,

    this.fat, this.sodium, this.carbohydrate);

    }  

    }

    NutritionFacts nutritionFacts = new NutritionFacts.Builder(10, 100)

    .calories(3).carbohydrate(2).fat(1).sodium(4).build();

    System.out.println(nutritionFacts.toString());

    /*******************************************/

    // A builder for objects of type T

    public interface Builder<T> {

    public T build();

    }

     

    Tree buildTree(Builder<? extends Node> nodeBuilder) { ... }

    Summary

    The Builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful(>4) of parameters. 

    作者:小郝
    出处:http://www.cnblogs.com/haokaibo/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Linux关闭防火墙命令
    js改变数组的两个元素的位子,互换、置顶
    vue nexttick的理解和使用场景
    vue mint-ui 框架下拉刷新上拉加载组件的使用
    vue项目中使用了vw适配方案,引入第三方ui框架mint-ui时,适配问题解决
    小程序开发笔记【二】,抽奖结果json数据拼装bug解决
    gulp插件gulp-nunjucks-render的使用及gulp4的简单了解
    小程序开发笔记【一】,查询用户参与活动列表 left join on的用法
    mysql数据插入前判断是否存在
    微信公众号通过图片选取接口上传到阿里oss
  • 原文地址:https://www.cnblogs.com/haokaibo/p/consider-a-builder-when-faced-with-many-constructor-parameters.html
Copyright © 2011-2022 走看看