一.介绍
1.建造者定义:
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示
用户只需指定需要建造的类型就可以得到他们,建造过程及细节不需要知道
2.类型:
创建型
3.适用场景:
如果一个对象有非常复杂的内部结构(很多属性)
想把复杂对象的创建和使用分离
4.优点:
封装性好,创建和使用分离
扩展性好,建造者之间独立,一定程度上解耦
5.缺点:
产生多余的Builder对象
产品内部发生变化,建造者都要修改,成本较大
二.demo演示
1.创建一个课程类和一些基本的属性
public class Course {
private String courseName;
private String coursePPT;
private String courseVideo;
private String courseArticle;
//问答
private String courseQA;
@Override
public String toString() {
return "Course{" +
"courseName='" + courseName + '\'' +
", coursePPT='" + coursePPT + '\'' +
", courseVideo='" + courseVideo + '\'' +
", courseArticle='" + courseArticle + '\'' +
", courseQA='" + 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;
}
}
2.创建一个CourseBuilder抽象类,并且创建一些接口和制作课程的接口
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();
}
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 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;
}
}
4.创建Coach教练类,相当于指挥官协助讲师进行视频的制作
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();
}
}
5.测试
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();
}
}
结果不进行展示了,但是上面的缺点就是属性非常多会导致属性值设置混乱,所以要进行下面的升级
三.demo升级演示
1.创建Course课程类,属性和构造方法,创建CourseBuilder静态内部类,添加课程的属性,创建对应属性赋值的方法,最终返回仍为本类CourseBuilder,创建Course类的构造方法,并且把CourseBuilder以参数的形式传入,然后把静态内部类的中属性的值设置给Course类的成员变量
public class Course {
private String courseName;
private String coursePPT;
private String courseVideo;
private String courseArticle;
//问答
private String courseQA;
@Override
public String toString() {
return "Course{" +
"courseName='" + courseName + '\'' +
", coursePPT='" + coursePPT + '\'' +
", courseVideo='" + courseVideo + '\'' +
", courseArticle='" + courseArticle + '\'' +
", courseQA='" + 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;
}
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);
}
}
}
2.测试
public class Test {
public static void main(String[] args) {
Course course = new Course.CourseBuilder().buildCourseName(“java设计模式”)
.buildCourseVideo(“java视频”).buildCoursePPT(“设计模式的PPT”)
.buildCourseArticle(“设计模式文章”).build();
System.out.println(course);
}
}
3.总结:
升级之后的方法好处就是会为对应的属性赋值,不会产生混乱的现象,原理就是,通过内部类的方法赋值给内部类的属性,然后在Course类的构造初始化的时候把内部类属性的值赋值给Course属性的值,最后通过builde方法返回当前已经赋过值的对象.
四.源码中的应用
1.在StringBuilder中的append方法使用的就是标准的建造者模式
@Override
public StringBuilder append(String str) {
super.append(str);
return this;
}
public StringBuilder append(StringBuffer sb) {
super.append(sb);
return this;
}
@Override
public StringBuilder append(CharSequence s) {
super.append(s);
return this;
}
还有很多append方法,想看的可以自己研究一下
2.Set的实现类ImmutableSet抽象类,看Builder类可以知道是一个标准的建造者模式
public static class Builder<E> extends ImmutableCollection.ArrayBasedBuilder<E> {
public Builder() {
this(DEFAULT_INITIAL_CAPACITY);
}
Builder(int capacity) {
super(capacity);
}
@CanIgnoreReturnValue
@Override
public Builder<E> add(E element) {
super.add(element);
return this;
}
.....
至此,建造者模式的讲解到此结束.