Design Mode 4- builder mode

definition

  1. 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.
  2. Users only need to specify the type of construction required to create them, the process of creating and details need to know.
  3. Type: Creating type

Applicable scene

  1. If an object has a very complex internal structure (multiple attributes)
  2. The creation and use of complex objects isolated

advantage

  1. Good package to create and use separate
  2. Develop good decoupling between the construction of class independence, to some extent,

Shortcoming

  1. Produce unwanted objects builder
  2. Internal product changes, builders need to be modified

The difference between the builder pattern and factory pattern

  1. Builder model to focus more on the method calling sequence, the factory model more focused on creating products
  2. 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.
  3. Factory mode only need to create it, the creator mode needs to know what are the components of the product.
  4. 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);


    }
}

Published 28 original articles · won praise 13 · views 120 000 +

Guess you like

Origin blog.csdn.net/qq_26483671/article/details/103943207