定义
将一个复杂对象的创建与它的表示分离,使得同样的构建过程可以创建不同的表示
用户只需要建造的类型就可以得到它们,建造过程及细节不需要知道
类型
创建型
适用场景
1、如果一个对象有非常复杂得内部结构(很多属性)
2、想把复杂对象的创建和使用分离
优点
1、封装性好,创建和使用分离
2、扩展性好、建造类之间独立、一定程度上解耦
缺点
1、产生多余的builder对象
2、产品内部发生变化,建造者都要修改,成本太大。
与工厂模式区别
1、建造者更注重方法调用顺序 工厂注重创建产品
2、创建对象的力度不同
建造者可以创建一些复杂的产品
3、关注点
工厂创建出来就好了 建造者不止要创建出来还要知道这个产品是那个部件组成的
代码
1、标准写法
//课程实例
public class Course {
private String courseName;
private String coursePPT;
private String courseVideo;
private String courseArticle;
private String courseQA;
//......Getter Setter toString
}
//课程抽象建造者
public abstract class CourseBuilder {
public abstract void buildCourseName(String courseName);
public abstract void buildCoursePPT(String coursePPT);
public abstract void buildCourseVideo(String courseVideo);
public abstract void buildCourseArticle(String courseArticle);
public abstract void buildCourseQA(String courseQA);
public abstract Course makeCourse();
}
//课程实体建造者
public class CourseActualBuilder extends CourseBuilder {
private Course course = new Course();
@Override
public void buildCourseName(String courseName) {
course.setCourseName(courseName);
}
@Override
public void buildCoursePPT(String coursePPT) {
course.setCoursePPT(coursePPT);
}
@Override
public void buildCourseVideo(String courseVideo) {
course.setCourseVideo(courseVideo);
}
@Override
public void buildCourseArticle(String courseArticle) {
course.setCourseArticle(courseArticle);
}
@Override
public void buildCourseQA(String courseQA) {
course.setCourseQA(courseQA);
}
@Override
public Course makeCourse() {
return course;
}
}
//指挥官用于制作课程
public class Coach {
private CourseBuilder courseBuilder;
public void setCourseBuilder(CourseBuilder courseBuilder) {
this.courseBuilder = courseBuilder;
}
public Course makeCourse(String courseName,
String coursePPT,
String courseVideo,
String courseArticle,
String courseQA) {
this.courseBuilder.buildCourseName(courseName);
this.courseBuilder.buildCoursePPT(coursePPT);
this.courseBuilder.buildCourseVideo(courseVideo);
this.courseBuilder.buildCourseArticle(courseArticle);
this.courseBuilder.buildCourseQA(courseQA);
return this.courseBuilder.makeCourse();
}
}
//测试
public class Test {
public static void main(String[] args) {
CourseBuilder courseBuilder = new CourseActualBuilder();
Coach coach = new Coach();
coach.setCourseBuilder(courseBuilder);
Course course = coach.makeCourse("java入门",
"javaPPT",
"javaVideo",
"javaArticle",
"javaQA"
);
System.out.println(course);
}
}
UML类图
2、优化写法
public class Course {
private String courseName;
private String coursePPT;
private String courseVideo;
private String courseArticle;
private String courseQA;
public Course(CourseBuilder courseBuilder) {
this.courseName = courseBuilder.courseName;
this.coursePPT = courseBuilder.coursePPT;
this.courseVideo = courseBuilder.courseVideo;
this.courseArticle = courseBuilder.courseArticle;
this.courseQA = courseBuilder.courseQA;
}
@Override
public String toString() {
return "Course{" +
"courseName='" + courseName + '\'' +
", coursePPT='" + coursePPT + '\'' +
", courseVideo='" + courseVideo + '\'' +
", courseArticle='" + courseArticle + '\'' +
", courseQA='" + courseQA + '\'' +
'}';
}
public static class CourseBuilder{
private String courseName;
private String coursePPT;
private String courseVideo;
private String courseArticle;
private String courseQA;
public CourseBuilder buildCourseName(String courseName){
this.courseName = courseName;
return this;
}
public CourseBuilder buildCoursePPT(String coursePPT){
this.coursePPT = coursePPT;
return this;
}
public CourseBuilder buildCourseVideo(String courseVideo){
this.courseVideo = courseVideo;
return this;
}
public CourseBuilder buildCourseArticle(String courseArticle){
this.courseArticle = courseArticle;
return this;
}
public CourseBuilder buildCourseQA(String courseQA){
this.courseQA = courseQA;
return this;
}
public Course build(){
return new Course(this);
}
}
}
//测试
public class Test {
public static void main(String[] args) {
Course course = new Course.CourseBuilder().
buildCourseName("Python课程").
buildCourseArticle("Py手记").
buildCoursePPT("PyPPt").
buildCourseQA("PyQA").buildCourseVideo("Py视频").build();
System.out.println(course );
}
}