建造者设计模式功能上很像元素的set方法,他实现了链式编程,使得一个复杂对象的构建过程和他的表示分离,属于创建型设计模式。
public class Student {
Student student = new Student();
private String id;
private String clazz;
private String age;
private String number;
public Student appentId (String id){
this.student.id = id;
return this;
}
public Student appentClazz (String clazz){
this.student.clazz = clazz;
return this;
}
public Student appentAge (String age){
this.student.age = age;
return this;
}
public Student appentNumber (String number){
this.student.clazz = number;
return this;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
调用方
public static void main(String[] args) {
//感觉好像和实际赋值也没啥区别,就代码更简洁了。
Student student = new Student();
student.appentId("123")
.appentAge("15")
.appentClazz("高三二班")
.appentNumber("0328");
student.setAge("15");
student.setClazz("高三二班");
student.setId("123");
}