1 代码演练
1.1 建造者模式演练
1.2 静态内部类演练建造者模式(链式调用)
1 代码演练
1.1 建造者模式演练
需求:
根据讲师提供的课程名称,课程ppt,课程视频,课程手记,课程问答 制作教学课程。
重点:
教练类coach.java标红部分,builder设值,然后赋值(看test.java)。
uml类图:
测试类:
package com.geely.design.pattern.creational.builder; public class Test { public static void main(String [] args){ Coach coach = new Coach(); Course course = new Course(); CourseBuilder courseBuilder = new CourseActualBuilder(); coach.setCourseBuilder(courseBuilder); course=coach.makeCourse("设计模式","设计模式PPT","设计模式标题","设计模式视频","设计模式课程问答"); System.out.println(course.toString()); } }
实体课程类:
package com.geely.design.pattern.creational.builder; public class Course { private String courseName; private String coursePPT; private String courseArticle; private String courseVideo; private String courseQA; public void setCourseName(String courseName) { this.courseName = courseName; } public void setCoursePPT(String coursePPT) { this.coursePPT = coursePPT; } public void setCourseArticle(String courseArticle) { this.courseArticle = courseArticle; } public void setCourseVideo(String courseVideo) { this.courseVideo = courseVideo; } public void setCourseQA(String courseQA) { this.courseQA = courseQA; } /** * toString方法,为了方便测试,进行打印 * @return */ @Override public String toString() { return "Course{" + "courseName='" + courseName + '\'' + ", coursePPT='" + coursePPT + '\'' + ", courseArticle='" + courseArticle + '\'' + ", courseVideo='" + courseVideo + '\'' + ", courseQA='" + courseQA + '\'' + '}'; } }
教练类:
package com.geely.design.pattern.creational.builder; public class Coach { CourseBuilder courseBuilder; public void setCourseBuilder(CourseBuilder courseBuilder) { this.courseBuilder = courseBuilder; } public Course makeCourse(String courseName, String coursePPT, String courseArticle, String courseVideo, String courseQA){ this.courseBuilder.buildCourseName(courseName); this.courseBuilder.buildCourseArticle(courseArticle); this.courseBuilder.buildCourseVideo(courseVideo); this.courseBuilder.buildCoursePPT(coursePPT); this.courseBuilder.buildCourseQA(courseQA); return this.courseBuilder.makeCourse(); } }
建造实现类:
package com.geely.design.pattern.creational.builder; 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 buildCourseArticle(String courseArticle) { course.setCourseArticle(courseArticle); } @Override public void buildCourseVideo(String courseVideo) { course.setCourseVideo(courseVideo); } @Override public void buildCourseQA(String courseQA) { course.setCourseQA(courseQA); } @Override public Course makeCourse() { return this.course; } }
建造抽象类:
package com.geely.design.pattern.creational.builder; /** * 这里使用 抽象类,完全是我练手用的,用实时接口也可以 */ public abstract class CourseBuilder{ public abstract void buildCourseName(String courseName); public abstract void buildCoursePPT(String coursePPT); public abstract void buildCourseArticle(String courseArticle); public abstract void buildCourseVideo(String courseVideo); public abstract void buildCourseQA(String courseQA); public abstract Course makeCourse(); }
打印结果:
"C:\Program Files\Java\jdk1.7.0_79\bin\java.exe" "-javaagent:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar=12439:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_79\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\rt.jar;F:\xiangmu3\Xin\Idea\design_pattern\target\classes" com.geely.design.pattern.creational.builder.Test Course{courseName='设计模式', coursePPT='设计模式PPT', courseArticle='设计模式标题', courseVideo='设计模式视频', courseQA='设计模式课程问答'} Process finished with exit code 0
1.2 静态内部类演练建造者模式(链式调用)
uml类图:
和1.1 区别:
1.2 可以少设值,而1.1不可以
自己理解:
静态内部类builder有5个属性,课程类有相同的5个属性,链式调用的时候,给builder全部或者部分赋值,build的时候,把builder对象传送到course,course获取到builder的属性,然后返回这个course;
测试类:
package com.geely.design.pattern.creational.builder.v2; public class Test { public static void main(String [] args){ Course.CourseBuilder courseBuilder = new Course.CourseBuilder(); Course course = courseBuilder.buildCourseName("设计模式课程标题").buildCourseVideo("设计模式视频").build(); System.out.println(course); } }
课程类:(静态内部类链式调用)
package com.geely.design.pattern.creational.builder.v2; public class Course { private String courseName; private String coursePPT; private String courseArticle; private String courseVideo; private String courseQA; public Course(CourseBuilder courseBuilder){ this.courseName = courseBuilder.courseName; this.courseArticle = courseBuilder.courseArticle; this.coursePPT = courseBuilder.coursePPT; this.courseVideo = courseBuilder.courseVideo; this.courseQA = courseBuilder.courseQA; } /** * toString方法,为了方便测试,打印对象的时候,能够打印出对象的属性 * @return */ @Override public String toString() { return "Course{" + "courseName='" + courseName + '\'' + ", coursePPT='" + coursePPT + '\'' + ", courseArticle='" + courseArticle + '\'' + ", courseVideo='" + courseVideo + '\'' + ", courseQA='" + courseQA + '\'' + '}'; } /** * 编写静态内部类,实现链式调用 */ public static class CourseBuilder{ private String courseName; private String coursePPT; private String courseArticle; private String courseVideo; 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 buildCourseArticle(String courseArticle) { this.courseArticle = courseArticle; return this; } public CourseBuilder buildCourseVideo(String courseVideo) { this.courseVideo = courseVideo; return this; } public CourseBuilder buildCourseQA(String courseQA) { this.courseQA = courseQA; return this; } public Course build(){ return new Course(this); } } }
打印日志:
"C:\Program Files\Java\jdk1.7.0_79\bin\java.exe" "-javaagent:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar=13237:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_79\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\rt.jar;F:\xiangmu3\Xin\Idea\design_pattern\target\classes" com.geely.design.pattern.creational.builder.v2.Test Course{courseName='设计模式课程标题', coursePPT='null', courseArticle='null', courseVideo='设计模式视频', courseQA='null'} Process finished with exit code 0
诸葛