本文参考

本篇文章参考自《Effective Java》第三版第二条"Consider a builder when faced with many constructor parameters"

静态工厂方法和构造器的不足之处

当一个类中有大量的字段时,尽管能通过设置不同的形参列表和方法名进行重载或使用静态工厂方法进行一定的简化,但我们还是会难以避免地为方法签名设置了较长的形参列表并直接使用到这些方法,以致于"it is hard to write client code when there are many parameters, and harder still to read it",原文将这种颇为不优美的解决方案称为"telescoping constructor pattern"(重叠构造器模式)

另一种不优美的解决方案是"JavaBean pattern"(JavaBean模式),我们需要频繁地调用set方法来构造我们需要的实例,只要有一个set方法没有调用,那么构造出来的实例就是不完整的,而且JavaBean模式使得把类做成不可变的可能性不复存在

a JavaBean may be in an inconsistent state partway through its construction and the JavaBeans pattern precludes the possibility of making a class immutable

Builder pattern(建造者模式)示例

我们不直接创建我们需要的实例,首先仅设置我们必需的字段值,返回一个Builder 实例

然后,上一步返回的Builder 实例调用类似set的方法来设置其它可选的字段值,同样,返回的都是Builder 实例

最后,Builder实例调用无参的build()方法,生成我们真正需要的实例,这种设计模式实现了类的不可变性

Instead of making the desired object directly, the client calls a constructor (or static factory) with all of the required parameters and gets a builder object.

Then the client calls setter-like methods on the builder object to set each optional parameter of interest.

Finally, the client calls a parameterless build method to generate the object, which is typically immutable.

The builder is typically a static member class of the class it builds.

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 sodium = 0;

    private int carbohydrate = 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 sodium(int val) {

      sodium = val;

      return this;
    }

    public Builder carbohydrate(int val) {

      carbohydrate = 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();
  }
}

NutritionFacts类的字段在Builder静态内部类中有一份一模一样的声明,并且默认参数值都在Builder类中,NutritionFacts类本身不具备任何为字段"直接"赋值的构造方法,而是由Builder实例"间接"完成

注意Builder的所有"set"方法都返回Builder的实例,使得我们可以链式调用其它"set"方法,原文将这种特性称为"fluent API",我们还可以根据具体情况判断是否需要为Builder的方法检查传入参数的有效性

The NutritionFacts class is immutable, and all parameter default values are in one place. The builder's setter methods return the builder itself so that invocations can be chained, resulting in a fluent API

The Builder pattern is well suited to class hierarchies

Builder类也适用于类层次结构,抽象类有抽象的 builder,具体类有具体的 builder,下面是原文抽象类的示例

abstract class Pizza {

  public enum Topping {HAM, MUSHROOM, ONION, PEPPER, SAUSAGE}

  final Set<Topping> toppings;

  // recursive type parameter
  abstract static class Builder<T extends Builder<T>> {

    EnumSet<Topping> toppings = EnumSet.noneOf(Topping.class);

    public T addTopping(Topping topping) {

      toppings.add(Objects.requireNonNull(topping));

      return self();
    }

    abstract Pizza build();

    // Subclasses must override this method to return "this"

    protected abstract
T self();
  }

  Pizza(Builder<?> builder) {

    toppings = builder.toppings.clone();
  }
}

下面是两个具体的实现类

class NyPizza extends Pizza {

  public enum Size {SMALL, MEDIUM, LARGE}

  private final Size size;

  public static class Builder extends Pizza.Builder<Builder> {

    private final Size size;

    public Builder(Size size) {

      this.size = Objects.requireNonNull(size);
    }

    @Override

    public NyPizza build() {

      return new NyPizza(this);
    }

    @Override

    protected Builder self() {

      return this;
    }
  }

  private NyPizza(Builder builder) {

    super(builder);

    size = builder.size;
  }
}

class Calzone extends Pizza {

  private final boolean sauceInside;

  public static class Builder extends Pizza.Builder<Builder> {

    private boolean sauceInside = false; // Default

    public
Builder sauceInside() {

      sauceInside = true;

      return this;
    }

    @Override

    public Calzone build() {

      return new Calzone(this);
    }

    @Override

    protected Builder self() {

      return this;
    }
  }

  private Calzone(Builder builder) {

    super(builder);

    sauceInside = builder.sauceInside;
  }
}

注意在子类中,build()方法的返回类型是子类本身,而不是抽象类,否则就需要我们在调用build()方法后,将返回的抽象类进行强制类型转换

Effective Java —— 多字段下考虑使用建造者模式构建实例的更多相关文章

  1. java的23种设计模式之建造者模式

    场景和本质 场景 本质 案例 原理 应用场景 场景和本质 场景 我们要建造一个复杂的产品.比如:神州飞船,Iphone.这个复杂的产品的创建.有这样一个问题需要处理:装配这些子组件是不是有个步骤问题? ...

  2. Java设计模式(四)Builder建造者模式

    一.场景描述 建造者模式同工厂模式.抽象工厂模式一样,用于创建继承类对象. 工厂模式:http://www.cnblogs.com/mahongbiao/p/8618970.html 抽象工厂模式:h ...

  3. JAVA设计模式之简单粗暴学建造者模式

    文章由浅入深,先用简单例子说明建造者,然后分析模式的优缺点,最后结合优秀开源框架Mybatis,说明该模式的用处. 1.先定义一个机器人模型 package com.jstao.model; publ ...

  4. java设计模式(二)单例模式 建造者模式

    (三)单例模式 单例模式应该是最常见的设计模式,作用是保证在JVM中,该对象仅仅有一个实例存在. 长处:1.降低某些创建比較频繁的或者比較大型的对象的系统开销. 2.省去了new操作符,减少系统内存使 ...

  5. Java学习笔记——设计模式之九.建造者模式

     建造者模式(Builder),将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. Product类: package cn.happy.design_pattern._09b ...

  6. Java设计模式菜鸟系列(十五)建造者模式建模与实现

    转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39863125 建造者模式(Builder):工厂类模式提供的是创建单个类的模式.而建造者模 ...

  7. Java设计模式之(三)——建造者模式

    1.什么是建造者模式 Separate the construction of a complex object from its representation so that the same co ...

  8. java 建造者模式

    package de.bvb.test3; /** * 建造者模式: 假如一个类有4个字段,每个字段或者每几个字段的组合都需要设置为构造函数,构造函数就比较麻烦 * 而且如果再加一个字段进去也不好拓展 ...

  9. Java设计模式之建造者模式(Builder Pattern)

    前言 这篇文章主要向大家讲解什么是建造者模式,建造者模式的实例讲解及应用场景等知识点. 一.建造者介绍 ​ 用户可以不知道产品的构建细节直接可以创建复杂的对象,主要是分离了产品的构建和装配,这样就实现 ...

随机推荐

  1. C语言qsort()函数的使用

    C语言qsort()函数的使用 qsort()函数是 C 库中实现的快速排序算法,包含在 stdlib.h 头文件中,其时间复杂度为 O(nlogn).函数原型如下: void qsort(void ...

  2. Zookeeper集群搭建及原理

    1 概述 1.1 简介 ZooKeeper 是 Apache 的一个顶级项目,为分布式应用提供高效.高可用的分布式协调服务,提供了诸如数据发布/订阅.负载均衡.命名服务.分布式协调/通知和分布式锁等分 ...

  3. oj教程--向量容器

    vector向量容器不但像数组一样对元素进行随机访问,还能在尾部插入元素,是一个简单.高效的容器, 完全可以替代数组.vector具有内存自动管理的功能,对于元素的插入和删除,可动态调整所占用的内存空 ...

  4. mapreduce—shuffle图解

  5. Maven打包异常:Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war

    出现下面异常,因为默认web.xml在  src/main/webapp  下所以才出现找不到的异常. 我的项目结构为 解决办法①:  在pom.xml里面制定 web位置即可 //先确保打包方式为w ...

  6. Qt:QSqlDatabase

    0.说明 QSqlDatabase类处理与数据库连接相关的操作.一个QSqlDatabase实例就代表了一个连接,连接时要提供访问数据库的driver,driver继承自QSqlDriver. 通过静 ...

  7. C# 字符串、数组和List的截取和转换

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  8. jarvisoj_level3

    自己的第一篇博客 *其实很早就想写博客了但是自己安全学的比较晚而且也很菜,现在虽然还是比较菜但是也是有一些心得的 *第一个问题什么是ctf,根据某度来说:CTF(Capture The Flag)中文 ...

  9. laravel7 数据库数据导出至 xlsx

    网址参考: https://learnku.com/articles/32391 1:安装excel插件 安装方式 composer require maatwebsite/excel 2:excel ...

  10. linux定时任务 - crontab定时任务

    crontab 定时任务命令 linux 系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的.另 外, 由于使用者 ...