链式调用建造者模式
This commit is contained in:
@@ -0,0 +1,78 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.creational.builder.chain;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 实体类
|
||||||
|
* 用静态内部类建造者,实现链式调用(推荐)
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2018-09-25 17:13
|
||||||
|
*/
|
||||||
|
public class BuCourse {
|
||||||
|
private String courseName;
|
||||||
|
private String coursePPT;
|
||||||
|
private String courseVideo;
|
||||||
|
/**
|
||||||
|
* question & answer
|
||||||
|
*/
|
||||||
|
private String courseQA;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造器接受静态内部类
|
||||||
|
*
|
||||||
|
* @param builder
|
||||||
|
*/
|
||||||
|
public BuCourse(BuCourseBuilder builder) {
|
||||||
|
this.courseName = builder.courseName;
|
||||||
|
this.coursePPT = builder.coursePPT;
|
||||||
|
this.courseVideo = builder.courseVideo;
|
||||||
|
this.courseQA = builder.courseQA;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "BCourse{" +
|
||||||
|
"courseName='" + courseName + '\'' +
|
||||||
|
", coursePPT='" + coursePPT + '\'' +
|
||||||
|
", courseVideo='" + courseVideo + '\'' +
|
||||||
|
", courseQA='" + courseQA + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class BuCourseBuilder {
|
||||||
|
private String courseName;
|
||||||
|
private String coursePPT;
|
||||||
|
private String courseVideo;
|
||||||
|
private String courseQA;
|
||||||
|
|
||||||
|
public BuCourseBuilder courseName(String courseName) {
|
||||||
|
this.courseName = courseName;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BuCourseBuilder coursePPT(String coursePPT) {
|
||||||
|
this.coursePPT = coursePPT;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BuCourseBuilder courseVideo(String courseVideo) {
|
||||||
|
this.courseVideo = courseVideo;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BuCourseBuilder courseQA(String courseQA) {
|
||||||
|
this.courseQA = courseQA;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 传递静态内部类返回目标对象
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public BuCourse build() {
|
||||||
|
return new BuCourse(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package top.fjy8018.designpattern.pattern.creational.builder;
|
|||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import top.fjy8018.designpattern.pattern.creational.builder.chain.BuCourse;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
@@ -14,4 +15,15 @@ class CourseBuilderTest {
|
|||||||
BCourse course = coach.createCourse("Java课程", "课程PPT", "课程视频", "课程问答");
|
BCourse course = coach.createCourse("Java课程", "课程PPT", "课程视频", "课程问答");
|
||||||
log.info(course.toString());
|
log.info(course.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void chainBuild() {
|
||||||
|
BuCourse course = new BuCourse.BuCourseBuilder()
|
||||||
|
.courseName("Java课程")
|
||||||
|
.coursePPT("课程PPT")
|
||||||
|
.courseVideo("课程视频")
|
||||||
|
.courseQA("课程问答")
|
||||||
|
.build();
|
||||||
|
log.info(course.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user