类图
代码
被建造者
public class Course {
private String courseName;
private String courseVideo;
private String courseArticle;
private String coursePPT;
private String courseQA;
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
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 getCoursePPT() {
return coursePPT;
}
public void setCoursePPT(String coursePPT) {
this.coursePPT = coursePPT;
}
public String getCourseQA() {
return courseQA;
}
public void setCourseQA(String courseQA) {
this.courseQA = courseQA;
}
@Override
public String toString() {
return "Course{" +
"courseName='" + courseName + '\'' +
", courseVideo='" + courseVideo + '\'' +
", courseArticle='" + courseArticle + '\'' +
", coursePPT='" + coursePPT + '\'' +
", courseQA='" + courseQA + '\'' +
'}';
}
}
抽象建造者
public abstract class CourseBuilder {
public abstract void buildCourseName(String courseName);
public abstract void buildCourseVideo(String courseVideo);
public abstract void buildCourseArticle(String courseArticle);
public abstract void buildCoursePPT(String coursePPT);
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 buildCourseVideo(String courseVideo) {
course.setCourseVideo(courseVideo);
}
@Override
public void buildCourseArticle(String courseArticle) {
course.setCourseArticle(courseArticle);
}
@Override
public void buildCoursePPT(String coursePPT) {
course.setCoursePPT(coursePPT);
}
@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 courseVideo,
String courseArticle,
String coursePPT,
String courseQA) {
courseBuilder.buildCourseArticle(courseArticle);
courseBuilder.buildCourseName(courseName);
courseBuilder.buildCourseVideo(courseVideo);
courseBuilder.buildCoursePPT(coursePPT);
courseBuilder.buildCourseQA(courseQA);
return courseBuilder.makeCourse();
}
}
客户端
public class Test {
public static void main(String[] args) {
Coach coach = new Coach();
CourseBuilder courseBuilder = new CourseActualBuilder();
coach.setCourseBuilder(courseBuilder);
Course course = coach.makeCourse("Java设计模式名称",
"Java设计模式视频",
"Java设计模式手记",
"Java设计模式PPT",
"Java设计模式问答");
System.out.println(course);
}
}
输出:
Course{courseName='Java设计模式名称', courseVideo='Java设计模式视频', courseArticle='Java设计模式手记', coursePPT='Java设计模式PPT', courseQA='Java设计模式问答'}