设计模式:建造者模式

2019-07-19  本文已影响0人  谁家的猪

定义与类型

定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示
用户只需指定需要建造的类型就可以得到它们,建造过程及细节不需要指定
类型:创建型

适用场景

优点

缺点

代码示例

以创建课程为例

第一个版本

  1. 创建Course 类
import lombok.Data;

/**
 * @author lijiayin
 */
@Data
public class Course {
    
    private String courseName;
    
    private String coursePPT;
    
    private String courseVideo;
    
    private String courseArticle;
    
    private String courseQA;
}
  1. 创建CourseBuilder 抽象类
/**
 * @author lijiayin
 */
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();
}
  1. 创建CourseActualBuilder 类
/**
 * @author lijiayin
 */
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;
    }
}
  1. 创建Coach类
import lombok.Setter;

/**
 * @author lijiayin
 */
@Setter
public class Coach {
    
    private 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();
    }
}
  1. 创建测试类
/**
 * @author lijiayin
 */
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);
    }
}
  1. 测试结果


    测试结果.png
  2. UML图


    UML.png

第二个版本

  1. 创建Course类
import lombok.Data;

/**
 * @author lijiayin
 */
@Data
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.coursePPT = courseBuilder.coursePPT;
        this.courseVideo = courseBuilder.courseVideo;
        this.courseArticle = courseBuilder.courseArticle;
        this.courseQA = courseBuilder.courseQA;
    }

    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);
        }
        
    }
}
  1. 创建测试类
/**
 * @author lijiayin
 */
public class Test {

    public static void main(String[] args) {
        Course course = new Course.CourseBuilder()
                .buildCourseName("Java设计模式")
                .buildCoursePPT("Java设计模式PPT")
                .buildCourseVideo("Java设计模式Video")
                .buildCourseArticle("Java设计模式笔记")
                .buildCourseQA("Java设计模式问答")
                .build();
        System.out.println(course);
    }
}
  1. 测试结果


    测试结果.png
  2. UML图


    UML.png

源码示例

  1. jdk的StringBuffer
  2. guava的ImmutableSet
  3. spring的BeanDefinitionBuilder
上一篇下一篇

猜你喜欢

热点阅读