本篇从生活中实例开始,循序渐渐,一步步引入观察者模式。
2010刚上大学的时候,辅导员都会让学委统计下学生的信息,第一次的时候,只是让学委统计基本信息,姓名、学号、手机号,当拿到这个要求的时候,一般情况下我们会采用构造函数去实现,把姓名、学号、手机号作为参数传过去,然后打印出来就好,代码如下:
public class BuilderTest
{
public static void main(String[] args) {
Person person = new Person("zero", "31101037", "1866***2153");
person.print();
}
}
/**
* created by zero on 2016-08-19
*/
public class Person
{
private String name;
private String id;
private String phoneNumber;
public Person(String name, String id, String phoneNumber)
{
this.name = name;
this.id = id;
this.phoneNumber = phoneNumber;
}
public void print() {
System.out.println("姓名:" + name + "\n学号:" + id + "\n手机号码:"
+ phoneNumber);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
运行结果
姓名:zero
学号:31101037
手机号码:1866***2153
当学委统计完的时候,辅导员觉得应该再加上QQ号,方便以后建班级群,学委听了后,简单,加个字段就ok了,只需要把Person(String name, String id, String phoneNumber)改成Person(String name, String id, String phoneNumber,String qq)便可以,是的,多么机智的学委,当机智的学委遇到爱折腾的辅导员的时候,也是会醉了。
后来辅导员又要添加家庭地址、宿舍代号、班级代号、邮箱,2011年随着微信的推广,又加上了微信,随着一次次改版,这下学委有点恼火了,更恼火的是需求还在一点点的改,并且还有部分学生未开通微信和邮箱,辅导员的意思是姓名、学号、手机号码必填,其它的提供就填上,此时的构造函数显得好臃肿,看起来好恶心,要是有一个比较优雅的方式来解决这个问题那该多好,每次改动的时候,main函数也需要同时改动,好烦,如果可以在不影响主函数调用的情况下去增加字段,对于必要的字段设置成必传,非必传的字段可传可不传,天空一声巨响,建造者模式闪亮登场。
/**
* created by zero on 2016-08-19
*
* 建造者模式
*/
public class Person2
{
private final String name;
private final String id;
private final String phoneNumber;
private String address;
private String dormitoryId;
private String classId;
private String email;
private String weChatId;
public static class Buidler{
private final String name;
private final String id;
private final String phoneNumber;
private String address;
private String dormitoryId;
private String classId;
private String email;
private String weChatId;
public Buidler(String name, String id, String phoneNumber)
{
this.name = name;
this.id = id;
this.phoneNumber = phoneNumber;
}
/**
* 以下几个字段根据需要,可传可不传,可以把builder理解成一个容器,不断填充此容器,
* 填充完以后,把此时的builder传到当前类的构造函数中
*/
public Buidler setAddress(String address){
this.address = address;
return this;
}
public Buidler setDormitory(String dormitoryId){
this.dormitoryId = dormitoryId;
return this;
}
public Buidler setClassId(String classId){
this.classId = classId;
return this;
}
public Buidler setEmail(String email){
this.email = email;
return this;
}
public Buidler setWeChatId(String weChatId){
this.weChatId = weChatId;
return this;
}
public Person2 build(){
return new Person2(this);
}
}
public Person2(Buidler buidler)
{
this.name = buidler.name;
this.id = buidler.id;
this.phoneNumber = buidler.phoneNumber;
this.address = buidler.address;
this.dormitoryId = buidler.dormitoryId;
this.classId = buidler.classId;
this.email = buidler.email;
this.weChatId = buidler.weChatId;
}
public void print() {
StringBuffer sb = new StringBuffer();
sb.append("姓名:" + name + "\n");
sb.append("学号:" + id + "\n");
sb.append("手机号码:" + phoneNumber+"\n");
if(!isEmpty(address))
sb.append("地址:"+address+"\n");
if(!isEmpty(dormitoryId))
sb.append("宿舍号:"+dormitoryId+"\n");
if(!isEmpty(classId))
sb.append("班级号:"+classId+"\n");
if(!isEmpty(email))
sb.append("邮箱:"+email+"\n");
if(!isEmpty(weChatId))
sb.append("微信号:"+weChatId+"\n");
System.out.println(sb.toString());
}
/**
* 以下是引用Android中TextUtils中的isEmpty方法
* Returns true if the string is null or 0-length.
* @param str the string to be examined
* @return true if str is null or zero length
*/
public static boolean isEmpty(CharSequence str) {
if (str == null || str.length() == 0)
return true;
else
return false;
}
}
- 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
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
public class BuilderTest
{
/**
* created by zero on 2016-08-19
*/
public static void main(String[] args) {
Buidler buidler = new Buidler("zero", "31101037", "1866***2153");
buidler.setClassId("311010");
buidler.setDormitory("X9-107");
Person2 person2 = buidler.build();
person2.print();
System.out.println("-----------------------分割线--------------------------");
Person2 person = new Buidler("first", "31101001", "1709***9816").setAddress("淮安市清浦区枚乘路信息学院").setClassId("311010").build();
person.print();
System.out.println("-----------------------分割线--------------------------");
new Buidler("ten", "31101010", "1327***3917").setEmail("596878238@qq.com").setClassId("311010").build().print();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
运行结果
姓名:zero
学号:31101037
手机号码:1866***2153
宿舍号:X9-107
班级号:311010
-----------------------分割线--------------------------
姓名:first
学号:31101001
手机号码:1709***9816
地址:淮安市清浦区枚乘路信息学院
班级号:311010
-----------------------分割线--------------------------
姓名:ten
学号:31101010
手机号码:1327***3917
班级号:311010
邮箱:596878238@qq.com
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
对于这些不订参数的构造函数,瞬间是不是觉得建造者模式就是救世主,减少了太多代码的开支和维护,减少了很多困扰,更有利于以后代码的扩展。当然构造函数也并不是一无是处,在参数固定不变的时候,构造函数显得更方便了一点,可读性也会相对而言更高一点。
本篇目的只要在于引进建造者模式,下篇会深入讲解,敬请关注,O(∩_∩)O哈哈~