一、重要概念
1、建造者模式与工厂模式的区别?
当创造一个对象,需要很多步骤时,适合建造者模式。当需要一个简单的方法,就可以简单的创建整个对象时,适合使用工厂相关模式
2、如何通过Set方式注入类到当前类?
3、创建者模式的流程及步骤
1).创建一个课程类
实现get set方法及重写tostring方法
2).课程的抽象建造者
抽象建造者实现属性的一个个创建,及一个类的创建
3).课程的实体建造者
实现类
4).大白话
抽象建造者实现一个个属性的创建方法(属性的抽象方法),及一个类的创建方法
课程属性的抽象方法写完了,还要实现一个抽象方法,返回这个课程:很简单,课程的每个属性制作完了以后,就要制作这个课程
4、创建者模式的几种实现
1)普通方式实现:实体类、建造者类、实体建造者类
2)教练方式实现:普通方式实现+教练类
3)静态内部类方式实现(静态内部类+链式调用): 实体类 、建造者类、
5、静态内部类的建造者模式?
把具体的实体类,和实体类对应的builder创建在一个类当中(静态内部类)
1)实现链式调用
Return this来实现链式调用
2)最大特点
最大特点,可以按需调用,并且按需调用后可以链式调用。
此方式实现,平时使用的比较多
二、创建者模式
建造者定义与类型
建造者的使用场景
建造者的优缺点
建造者UML类图
教练模式
Course实体类
package com.geely.design.pattern.creational.builder;
/**
* Created by geely
*/
public class Course {
private String courseName;
private String coursePPT;
private String courseVideo;
private String courseArticle;
//question & answer
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 com.geely.design.pattern.creational.builder;
/**
* Created by geely
*/
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();
}
具体工厂类
package com.geely.design.pattern.creational.builder;
/**
* Created by geely
*/
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;
}
}
教练类
package com.geely.design.pattern.creational.builder;
/**
* Created by geely
*/
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();
}
}
客户端调用类
package com.geely.design.pattern.creational.builder;
/**
* Created by geely
*/
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 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;
//question & answer
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);
}
}
}
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);
Set<String> set = ImmutableSet.<String>builder().add("a").add("b").build();
System.out.println(set);
}
}