4、教师类 ( 非必须 )
package builder; /** * 教师类 , 负责使用建造者创建学生信息 */ public class Teacher { /** * 持有学生建造者 */ private StudentBuilder studentBuilder; /** * 构造函数注入建造者 * @param studentBuilder */ public Teacher(StudentBuilder studentBuilder) { this.studentBuilder = studentBuilder; } /** * 调用建造者对象创建 学生 实例对象 * @param name * @param age * @param number * @param address * @param school * @return */ public Student makeStudent(String name, String age, String number, String address, String school) { this.studentBuilder.buildName(name); this.studentBuilder.buildAge(age); this.studentBuilder.buildNumber(number); this.studentBuilder.buildAddress(address); this.studentBuilder.buildSchool(school); return this.studentBuilder.makeCourse(); } }
5、测试类
package builder;
public class Main { public static void main(String[] args) { // 创建学生对象建造者 StudentBuilder studentBuilder = new StudentBuilderImpl(); // 创建教师对象 Teacher teacher = new Teacher(studentBuilder); // 教师对象使用学生建造者创建学生 实例对象 Student student = teacher.makeStudent( "小明", "12" , "1", "学院路", "第一小学"); System.out.println(student); } }
执行结果 :
Student{name='小明', age='12', number='1', address='学院路', school='第一小学'}
六、实例类和建造者类定义在同一个类中 ( 建造者模式常用用法 )
1、实例类 + 建造者类
package builder2;
/** * 学生类 */ public class Student { private String name; private String age; private String number; private String address; private String school; public Student(StudentBuilder builder) { this.name = builder.name; this.age = builder.age; this.number = builder.number; this.address = builder.address; this.school = builder.school; } public String getName() { return name; } public void setName(String name) { this.name = name; } 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 String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age='" + age + '\'' + ", number='" + number + '\'' + ", address='" + address + '\'' + ", school='" + school + '\'' + '}'; } /** * 使用静态内部类作为创建者 * 将具体的业务类 及其 创建者 定义在一个类中 * 使用链式调用 , 每个 build 步骤都返回创建者本身 */ public static class StudentBuilder { private String name; private String age; private String number; private String address; private String school; public StudentBuilder buildName(String name) { this.name = name; return this; } public StudentBuilder buildAge(String age) { this.age = age; return this; } public StudentBuilder buildNumber(String number) { this.number = number; return this; } public StudentBuilder buildAddress(String address) { this.address = address; return this; } public StudentBuilder buildSchool(String school) { this.school = school; return this; } public Student build() { return new Student(this); } } }
2、测试类
package builder2; public class Main { public static void main(String[] args) { // 使用链式调用 , 一行代码创建实例对象 Student student = new Student.StudentBuilder() .buildName("小明") .buildAge("12") .buildNumber("1") .buildAddress("学院路") .buildSchool("第一小学") .build(); System.out.println(student); } }
执行结果 :
Student{name='小明', age='12', number='1', address='学院路', school='第一小学'}