概要
创建对象时,提升属性设置的灵活性。
灵活性
类中定义了大量属性时,通常为了创建对象时属性初始设置的便利随之定义大量的构造方法。
为了既不定义过多的构造方法,又保证属性设置的便利性。
接下来我们看一个需要改进的案例。
对象创建的优化
现在有个Employee
类,如下。
最初版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| public class Employee { private String name; private String sex; private int age; private String address; private String post; private String company; private String department;
public Employee(String name, String sex, int age, String address, String post, String company, String department) { this.name = name; this.sex = sex; if (!("男".equals(sex) || "女".equals(sex))) { throw new RuntimeException("输入错误的性别:" + sex); } this.age = age; if (age <= 1 || age >= 150) { throw new RuntimeException("输入错误的年龄:" + age); } this.address = address; this.post = post; if (!postCheck()) { throw new RuntimeException("地址(" + address + ")与邮编(" + post + ")不一致"); } this.company = company; this.department = department; }
private boolean postCheck() { if (address == null) { return false; } return true; } }
|
实际上在业务上并没有过多的问题,最重要的问题就是这个类的使用非常的麻烦。
首先,构造函数只有一个,如果不增加新的构造函数的话无法灵活的传入不同数量的参数。并且为了让其参数的设置变得灵活,我们必须重载非常多种不一样的构造函数,工程量巨大,且枯燥乏味。
为此,我们有了一个新的改进方案。
修改版v1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| public class Employee { private String name; private String sex; private int age; private String address; private String post; private String company; private String department; public Employee() {}
public void setName(String name) { this.name = name; }
public void setSex(String sex) { this.sex = sex; if (!("男".equals(sex) || "女".equals(sex))) { throw new RuntimeException("输入错误的性别:" + sex); } }
public void setAge(int age) { this.age = age; if (age <= 1 || age >= 150) { throw new RuntimeException("输入错误的年龄:" + age); } }
public void setAddress(String address) { this.address = address; }
public void setPost(String post) { this.post = post; if (!postCheck()) { throw new RuntimeException("地址(" + address + ")与邮编(" + post + ")不一致"); } }
public void setCompany(String company) { this.company = company; }
public void setDepartment(String department) { this.department = department; }
private boolean postCheck() { if (address == null) { return false; } return true; } }
|
改成了上述方式后确实使用起来方便不少,不想传入的参数不调用对应的set
方法就好了,并且也省去了大量构造方法的定义。但是,依旧有一个问题,我们来看看使用样例。
1 2 3 4 5 6
| public Client { public static void main(String[] args) { Employee e = new Employee(); e.setPost("121-1245-1231"); } }
|
由于设置post
的时候对于address
属性做了非空判断,所以代码书写时address
的设置一定要在post
之前,否则就会报错。这无疑增加了项目开发的难度。
既然构造函数麻烦,set
也存在一些问题。那我们如何优化Employee
对象的创建呢?看下面的样例。
修改版v2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| public class Employee { private String name; private String sex; private int age; private String address; private String post; private String company; private String department;
private Employee(Builder builder) { this.name = builder.name; this.sex = builder.sex; this.age = builder.age; this.address = builder.address; this.post = builder.post; this.company = builder.company; this.department = builder.department; }
public static Builder Builder() { return new Builder(); }
public static class Builder { private String name; private String sex; private int age; private String address; private String post; private String company; private String department;
public Builder name(String name) { this.name = name; return this; }
public Builder sex(String sex) { this.sex = sex; return this; }
public Builder age(int age) { this.age = age; return this; }
public Builder address(String address) { this.address = address; return this; }
public Builder post(String post) { this.post = post; return this; }
public Builder company(String company) { this.company = company; return this; }
public Builder department(String department) { this.department = department; return this; }
public Employee build() { if (!("男".equals(sex) || "女".equals(sex))) { throw new RuntimeException("输入错误的性别:" + sex); } if (age <= 1 || age >= 150) { throw new RuntimeException("输入错误的年龄:" + age); } if (!postCheck()) { throw new RuntimeException("地址(" + address + ")与邮编(" + post + ")不一致"); } return new Employee(this); }
private boolean postCheck() { if (address == null) { return false; } return true; } } }
|
使用建造者模式优化对象的创建。客户端中对象的创建不再使用new
关键字,不需要定义数量繁多的构造函数以应对复杂多变的属性设置,并且也有着set
的灵活性又不存在属性设置顺序的依赖。以下是使用样例。
1 2 3 4 5 6
| public Client { public static void main(String[] args) { Employee e = Employee.Builder().name("张三").sex("男").age(35) .post("124-1241-1352").address("江西省南昌市").build(); } }
|
流式编程的风格使得代码清晰舒爽。
总结
优点
- 无需定义大量的构造方法。
- 既有
set
方法的灵活性,又消除了set
可能出现的属性固定顺序设置的问题。
- 流式编程的风格,代码清晰简洁。
缺点
- 由于定义了静态内部类
Builder
,可能会使得系统的类数量激增,影响性能。
- 代码的理解难度增加。
适用场景
- 适用于属性很多的类的创建。
- 适用于对属性设置判断条件复杂的类的创建。尤其是属性设置对于其他属性有依赖的情况。