建造者模式

建造者模式

建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

一个 Builder 类会一步一步构造最终的对象。该 Builder 类是独立于其他对象的。

简介

意图:将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。

主要解决:主要解决在软件系统中,有时候面临着”一个复杂对象”的创建工作,其通常由各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法却相对稳定。

何时使用:一些基本部件不会变,而其组合经常变化的时候。

如何解决:将变与不变分离开。

关键代码:建造者:创建和提供实例,导演:管理建造出来的实例的依赖关系。

应用实例: 1、去肯德基,汉堡、可乐、薯条、炸鸡翅等是不变的,而其组合是经常变化的,生成出所谓的”套餐”。 2、JAVA 中的 StringBuilder。

优点

  • 建造者独立,易扩展。
  • 便于控制细节风险。

缺点

  • 产品必须有共同点,范围有限制。
  • 如内部变化复杂,会有很多的建造类。

使用场景

  • 需要生成的对象具有复杂的内部结构。
  • 需要生成的对象内部属性本身相互依赖。

注意事项:与工厂模式的区别是:建造者模式更加关注与零件装配的顺序。

场景例子

课程的制作由名字,PPT,视频,文章,问答组成。

UML(v1)

代码

课程基类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.anthony.design.pattern.creational.builder;

/**
* @ Description: Course 基类
* @ Date: Created in 10:08 2018-11-27
* @ Author: Anthony_Duan
*/
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 + '\'' +
'}';
}
}

课程创建者抽象类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.anthony.design.pattern.creational.builder;

/**
* @ Description:
* @ Date: Created in 10:11 2018-11-27
* @ Author: Anthony_Duan
*/
public abstract class CourseBuilder {
public abstract void buildCourseName(String courseName);

public abstract void buildCoursePPT(String coursePPT);

public abstract void buildCourseArticle(String courseArticle);

public abstract void buildCourseVideo(String courseVideo);

public abstract void buildCourseQA(String courseQA);

public abstract Course makeCourse();


}

课程实际创建者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.anthony.design.pattern.creational.builder;

/**
* @ Description: CourseActualBuilder类
* @ Date: Created in 10:25 2018-11-27
* @ Author: Anthony_Duan
*/
public class CourseActualBuilder extends CourseBuilder {

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 buildCourseArticle(String courseArticle) {
course.setCourseArticle(courseArticle);
}

@Override
public void buildCourseVideo(String courseVideo) {
course.setCourseVideo(courseVideo);
}

@Override
public void buildCourseQA(String courseQA) {
course.setCourseQA(courseQA);
}

@Override
public Course makeCourse() {
return course;
}
}

教练类(导演类)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.anthony.design.pattern.creational.builder;

/**
* @ Description:
* @ Date: Created in 10:41 2018-11-27
* @ Author: Anthony_Duan
*/
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.buildCourseArticle(courseArticle);
this.courseBuilder.buildCourseQA(courseQA);
this.courseBuilder.buildCourseVideo(courseVideo);
return this.courseBuilder.makeCourse();
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.anthony.design.pattern.creational.builder;

/**
* @ Description: 测试类
* @ Date: Created in 10:48 2018-11-27
* @ Author: Anthony_Duan
*/
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);
}
}

UML(v2)

代码(v2)

第二个版本与第一个版本的最大区别是将具体实现类作为了内部类客户端可以采用链式调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.anthony.design.pattern.creational.builder.V2;

/**
* @ Description:
* @ Date: Created in 10:56 2018-11-27
* @ Author: Anthony_Duan
*/
public class Course {
private String courseName;
private String coursePPT;
private String courseVideo;
private String courseArticle;

//question & answer
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);
}

}

}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.anthony.design.pattern.creational.builder.V2;

/**
* @ Description:
* @ Date: Created in 11:07 2018-11-27
* @ Author: Anthony_Duan
*/
public class Test {
public static void main(String[] args) {
//链式调用
Course course = new Course.CourseBuilder().buildCourseName("Java设计模式精讲").buildCoursePPT("Java设计模式精讲PPT").buildCourseVideo("Java设计模式精讲视频").build();
System.out.println(course);
}
}

-------------End Of This ArticleThank You For Reading-------------