definition
- Model builder is going to build a complex object with his representation separation, so that the construction of the same process can create different representations.
- Users only need to specify the type of construction required to create them, the process of creating and details need to know.
- Type: Creating type
Applicable scene
- If an object has a very complex internal structure (multiple attributes)
- The creation and use of complex objects isolated
advantage
- Good package to create and use separate
- Develop good decoupling between the construction of class independence, to some extent,
Shortcoming
- Produce unwanted objects builder
- Internal product changes, builders need to be modified
The difference between the builder pattern and factory pattern
- Builder model to focus more on the method calling sequence, the factory model more focused on creating products
- Efforts to create objects of different, factory pattern to create complex products from a variety of components, create the same product out of the factory pattern.
- Factory mode only need to create it, the creator mode needs to know what are the components of the product.
- Applicable to the builder pattern when you create an object requires many steps, simply create the factory pattern can be.
Conding
Lombok way @Builder annotation plug-in provides the building object is typical of the builder pattern.
package com.example.design.creational.course.v2;
import lombok.Data;
import lombok.ToString;
/**
* 建造者模式 静态内部类
*
* @author wang
*/
@Data
@ToString
public class Course {
private String courseName;
private String coursePpt;
private String courseVideo;
private String courseArticle;
private String courseQa;
public Course(CourseBuilder courseBuilder) {
this.courseName = courseBuilder.courseName;
this.courseArticle = courseBuilder.courseArticle;
this.coursePpt = courseBuilder.coursePpt;
this.courseQa = courseBuilder.courseQa;
this.courseVideo = courseBuilder.courseVideo;
}
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);
}
}
}
It can be used as builder ways to create objects using
package com.example.design.creational.course.v2;
/**
* @author wang
*/
public class Test {
public static void main(String[] args) {
Course course = new Course.CourseBuilder().
buildCourseArticle("article").
buildCourseName("name").
buildCoursePpt("ppt").
buildCourseQa("qa").
buildCourseVideo("video")
.build();
System.out.println(course);
}
}