Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Definition and Code demonstration of Builder in java Design pattern

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "the definition and code demonstration of builder in java design pattern". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn the definition and code demonstration of the builder in the java design pattern.

1. The builder definition separates the construction of a complex object from its representation, so that the same construction process can create different representations. Users only need to specify the types that need to be created to get them, and the construction process and details do not need to be known. Applicable scenarios: 1) if an object has a very complex internal structure (many attributes) 2) want to separate the creation and use of complex objects: 1) good encapsulation, creation and use separation 2) good scalability, independence between construction classes, decoupling disadvantages to a certain extent: 1) generate redundant Builder objects 2) the builders have to modify the internal changes of the product High cost 2. Code demonstration 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) } public class Test {public static void main (String [] args) {Course course = new Course.CourseBuilder () .buildCourseName ("Java Design patterns focus") .buildCoursePPT ("Java Design patterns focus on PPT") .buildCourseVideo ("Java Design pattern intensive Video") .build () System.out.println (course); Set set = ImmutableSet.builder (). Add ("a"). Add ("b"). Build (); System.out.println (set);} so far, I believe you have a deeper understanding of "the definition and code demonstration of builders in java design patterns". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report