一,定义与类型(注重于方法的调用顺序)
1,定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示
2,用户只需指定需要建造的类型就可以得到他们,建造过程及细节不需要知道
3,类型:创建型
二,适用场景
1,如果一个对象有非常复杂的内部结构(很多属性)
2,想把复杂对象的创建和使用分离
三,优点
封装性好,创建和使用分离
扩展性好,建造者之间独立,一定程度上解耦
(不仅创建出产品,还关心由那些部件组成)
四,coding
1.Course
public class Course {
private String courseName;
private String coursePPT;
private String courseVedio;
private String courseArticle;
private String courseQA;
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getCoursePPT() {
return coursePPT;
}
public void setCoursePPT(String coursePPT) {
this.coursePPT = coursePPT;
}
public String getCourseVedio() {
return courseVedio;
}
public void setCourseVedio(String courseVedio) {
this.courseVedio = courseVedio;
}
public String getCourseArticle() {
return courseArticle;
}
public void setCourseArticle(String courseArticle) {
this.courseArticle = courseArticle;
}
public String getCourseQA() {
return courseQA;
}
public void setCourseQA(String courseQA) {
this.courseQA = courseQA;
}
@Override
public String toString() {
return "Course{" +
"courseName='" + courseName + '\'' +
", coursePPT='" + coursePPT + '\'' +
", courseVedio='" + courseVedio + '\'' +
", courseArticle='" + courseArticle + '\'' +
", courseQA='" + courseQA + '\'' +
'}';
}
}
2.CourseBuilder
public abstract class CourseBuilder {
public abstract void buildCourseName(String courseName);
public abstract void buildCoursePPT(String coursePPT);
public abstract void buildCourseVedio(String courseVedio);
public abstract void buildCourseArticle(String courseArticle);
public abstract void buildCourseQA(String courseQA);
public abstract Course makeCourse();
}
3.CourseActualBuilder
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 buildCourseVedio(String courseVedio) {
course.setCourseVedio(courseVedio);
}
@Override
public void buildCourseArticle(String courseArticle) {
course.setCourseArticle(courseArticle);
}
@Override
public void buildCourseQA(String courseQA) {
course.setCourseQA(courseQA);
}
@Override
public Course makeCourse() {
return course;
}
}
4.Coach
public class Coach {
private CourseBuilder courseBuilder;
public void setCourseBuilder(CourseBuilder courseBuilder) {
this.courseBuilder = courseBuilder;
}
public Course makeCourse(String courseName,String coursePPT,String courseVedio,
String courseArticle,String courseQA){
this.courseBuilder.buildCourseName(courseName);
this.courseBuilder.buildCourseArticle(courseArticle);
this.courseBuilder.buildCoursePPT(coursePPT);
this.courseBuilder.buildCourseVedio(courseVedio);
this.courseBuilder.buildCourseQA(courseQA);
return this.courseBuilder.makeCourse();
}
}
5.Test
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設計模式",
"JAVA設計模式PPT",
"JAVA設計模式視頻",
"JAVA設計模式手記",
"JAVA設計模式問答");
System.out.println(course);
}
}
版本二
链式
public class Course {
private String courseName;
private String coursePPT;
private String courseVedio;
private String courseArticle;
private String courseQA;
public Course(CourseBuilder courseBuilder){
this.courseArticle=courseBuilder.courseArticle;
this.courseName=courseBuilder.courseName;
this.courseVedio=courseBuilder.courseVedio;
this.coursePPT=courseBuilder.coursePPT;
this.courseQA=courseBuilder.courseQA;
}
@Override
public String toString() {
return "Course{" +
"courseName='" + courseName + '\'' +
", coursePPT='" + coursePPT + '\'' +
", courseVedio='" + courseVedio + '\'' +
", courseArticle='" + courseArticle + '\'' +
", courseQA='" + courseQA + '\'' +
'}';
}
public static class CourseBuilder{
private String courseName;
private String coursePPT;
private String courseVedio;
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 buildCourseVedio(String courseVedio) {
this.courseVedio=courseVedio;
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);
}
}
}
Test
public class Test {
public static void main(String[] args) {
Course course=new Course.CourseBuilder().buildCourseName("JAVA設計模式").buildCoursePPT("JAVA設計模式PPT").buildCourseVedio("JAVA設計模式視頻").build();
System.out.println(course);
}
}
五,应用
StringBuilder
StringBuffer
Spring里的BeanDefinitionBuilder
Mybatis里的SqlSessionFactoryBuilder