乐趣区

关于设计模式:设计模式建造者模式

建造者模式

  • 定义:将一个简单对象的构建与它的示意拆散,使得同样的构建过程能够创立不同的示意
  • 用户只需指定须要建造的类型就能够失去它们,建造过程及细节不须要晓得
  • 类型:创立型
  • 实用场景:

    • 如果一个对象有非常复杂的内部结构(很多属性)
    • 想把简单对象的创立和应用拆散
  • 长处:

    • 封装性好,创立和应用拆散
    • 扩展性好、建造类之间独立、肯定水平上解耦
  • 毛病:

    • 产生多余的 Builder 对象
    • 产品外部发生变化,建造者都要批改,老本较大
代码示例

版本一:

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 + '\'' +
                '}';
    }
}
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();}
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;}
}
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){courseBuilder.buildCourseName(courseName);
        courseBuilder.buildCoursePPT(coursePPT);
        courseBuilder.buildCourseVideo(courseVideo);
        courseBuilder.buildCourseArticle(courseArticle);
        courseBuilder.buildCourseQA(courseQA);
        return  courseBuilder.makeCourse();}
}

测试类:

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) {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);
    }
}
利用场景

JDK:

  • StringBuilder(线程不平安)
  • StringBuffer(线程平安)
  • guava(CacheBuilder)
  • Spring(BeanDefinitionBuilder)
  • Mybatis(SqlSessionFactoryBuilder(XMLConfigBuilder))
退出移动版