看到别人的代码顿觉精妙,记录留存。
应用场景:假设有一个问题,我们需要创建一个学生对象,属性有name,number,class,sex,age,school等属性,如果每一个属性都可以为空,也就是说我们可以只用一个name,也可以用一个school,name,或者一个class,number,或者其他任意的赋值来创建一个学生对象,这时该怎么构造?
难道我们写6个1个输入的构造函数,15个2个输入的构造函数.......吗?这个时候就需要用到Builder模式了。
public class Builder { static class Student{ String name = null ; int number = -1 ; String sex = null ; int age = -1 ; String school = null ; static class StudentBuilder{ String name = null ; int number = -1 ; String sex = null ; int age = -1 ; String school = null ; public StudentBuilder setName(String name) { this.name = name; return this ; } public StudentBuilder setNumber(int number) { this.number = number; return this ; } public StudentBuilder setSex(String sex) { this.sex = sex; return this ; } public StudentBuilder setAge(int age) { this.age = age; return this ; } public StudentBuilder setSchool(String school) { this.school = school; return this ; } public Student build() { return new Student(this); } } public Student(StudentBuilder builder){ this.age = builder.age; this.name = builder.name; this.number = builder.number; this.school = builder.school ; this.sex = builder.sex ; } } public static void main( String[] args ){ /** * Student.StudentBuilder()方法新建StudentBuilder静态类,然后调用setAge方法设置年龄,由于所有 * set方法都返回StudentBuilder类对象,所以可以一直调用,直至需要设置的属性设置完毕然后调用Student * 有参构造方法,以StudentBuilder为参数,将刚才所设置的参数一一赋值,关键在于每个set函数返回值都是 * 一个StudentBuilder对象,这样才能灵活调用所有设置方法 */ Student a = new Student.StudentBuilder().setAge(13).setName("LiHua").build(); Student b = new Student.StudentBuilder().setSchool("sc").setSex("Male").setName("ZhangSan").build(); } }
研究清楚了这个方法扩展思考:干嘛非得多建静态类,于是有了下面代码:
class Human { private String name; private int age; private String sex; public Human setName(String name) { this.name = name; return this; } public Human setAge(int age) { this.age = age; return this; } public Human setSex(String sex) { this.sex = sex; return this; } public Human Human(){ return this; } public String getName() { return name; } public int getAge() { return age; } public String getSex() { return sex; } } public class Builder { static class Student{ String name = null ; int number = -1 ; String sex = null ; int age = -1 ; String school = null ; static class StudentBuilder{ String name = null ; int number = -1 ; String sex = null ; int age = -1 ; String school = null ; public StudentBuilder setName(String name) { this.name = name; return this ; } public StudentBuilder setNumber(int number) { this.number = number; return this ; } public StudentBuilder setSex(String sex) { this.sex = sex; return this ; } public StudentBuilder setAge(int age) { this.age = age; return this ; } public StudentBuilder setSchool(String school) { this.school = school; return this ; } public Student build() { return new Student(this); } } public Student(StudentBuilder builder){ this.age = builder.age; this.name = builder.name; this.number = builder.number; this.school = builder.school ; this.sex = builder.sex ; } } public static void main( String[] args ){ /** * Student.StudentBuilder()方法新建StudentBuilder静态类,然后调用setAge方法设置年龄,由于所有 * set方法都返回StudentBuilder类对象,所以可以一直调用,直至需要设置的属性设置完毕然后调用Student * 有参构造方法,以StudentBuilder为参数,将刚才所设置的参数一一赋值,关键在于每个set函数返回值都是 * 一个StudentBuilder对象,这样才能灵活调用所有设置方法 */ Student a = new Student.StudentBuilder().setAge(13).setName("LiHua").build(); Student b = new Student.StudentBuilder().setSchool("sc").setSex("Male").setName("ZhangSan").build(); Human h = new Human().setAge(1).setName("张三"); } }
也能实现此功,并且看上去更简单点,或许原代码有其他拓展功能,各位求指教。