建造者模式的定义与类
- 定义:将一个复杂对象的构建与他的表示分离,使得同样的构建过程可以创建不同的表示
- 用户只需要指定他们创建的类型就可以得到他们,建造过程的细节不需要知道
- 类型:创建型
适用场景
- 如果一个对象有非常复杂的内部结构(很多属性)
- 想把复杂对象创建和使用分离
优点
- 封装性好,创建和使用分离
- 扩展性好、建造者之间独立、一定程度上 解耦
缺点
- 产生多个builder对象
- 产品内部发生变化,建造者都要修改,成本大
请看下面代码:
package design.creational.builder;
public abstract class CourseBuilder {
public abstract void buildCouresName(String courseName);
public abstract void buildCoursePPT(String coursePPT);
public abstract void buildCourseVideo(String courseVideo);
public abstract void buildCourseArticle(String courseArticle);
public abstract void buildCouresQA(String courseQA);
public abstract Course makeCouese();
}
package design.creational.builder;
public class CourseActualBuilder extends CourseBuilder{
private Course course = new Course();
@Override
public void buildCouresName(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 buildCouresQA(String courseQA) {
course.setCourseQA(courseQA);
}
@Override
public Course makeCouese() {
return course;
}
}
package design.creational.builder;
public class Course {
private String courseName;
private String coursePPT;
private String courseVideo;
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 getCourseVideo() {
return courseVideo;
}
public void setCourseVideo(String courseVideo) {
this.courseVideo = courseVideo;
}
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 + '\'' +
", courseVideo='" + courseVideo + '\'' +
", courseArticle='" + courseArticle + '\'' +
", courseQA='" + courseQA + '\'' +
'}';
}
}
package design.creational.builder;
public class Coach {
private CourseBuilder courseBuilder;
public void setCourseBuilder(CourseBuilder courseBuilder) {
this.courseBuilder = courseBuilder;
}
public Course makeCourse(String courseName,String coursePPT,
String courseViddeo,String courseArticle,
String courseQA){
this.courseBuilder.buildCouresName(courseName);
this.courseBuilder.buildCouresQA(courseQA);
this.courseBuilder.buildCoursePPT(coursePPT);
this.courseBuilder.buildCourseVideo(courseViddeo);
this.courseBuilder.buildCourseArticle(courseArticle);
return this.courseBuilder.makeCouese();
}
}
package design.creational.builder;
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",
"视频","手记","问题和答案");
System.out.println(course);
}
}
演进版
package design.creational.builder.v2;
import design.creational.builder.CourseBuilder;
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.courseArticle = courseBuilder.courseArticle;
this.coursePPT = courseBuilder.coursePPT;
this.courseQA = courseBuilder.courseQA;
this.courseVideo = courseBuilder.courseVideo;
}
public static class CourseBuilder {
private String courseName;
private String coursePPT;
private String courseVideo;
private String courseArticle;
private String courseQA;
public CourseBuilder builderCourseName(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 buildCouresQA(String courseQA) {
this.courseQA = courseQA;
return this;
}
public Course build(){
return new Course(this);
}
}
@Override
public String toString() {
return "Course{" +
"courseName='" + courseName + '\'' +
", coursePPT='" + coursePPT + '\'' +
", courseVideo='" + courseVideo + '\'' +
", courseArticle='" + courseArticle + '\'' +
", courseQA='" + courseQA + '\'' +
'}';
}
}
package design.creational.builder.v2;
public class Test {
public static void main(String[] args) {
Course course = new Course.CourseBuilder().buildCourseArticle("文章")
.buildCoursePPT("ppt").buildCourseVideo("视频").builderCourseName("java设计模式")
.buildCouresQA("问题和答案").build();
System.out.println(course);
}
}