In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you the relevant knowledge points about the case analysis of Java Builder pattern. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
Advantages
1. The builder is independent and easy to expand.
two。 It is easy to control the risk of details.
Shortcoming
1. Products must have something in common and limited in scope.
two。 If the internal changes are complex, there will be a lot of construction classes.
Working with scen
1. The objects that need to be generated have complex internal structures.
two。 The internal properties of the objects that need to be generated depend on each other.
1. Package com.asurplus.common.builder.style1;public class UserInfo {private String name; private int age; / * create a new builder * * @ return * / public static UserInfoBuilder builder () {return new UserInfoBuilder ();} public String getName () {return name;} public void setName (String name) {this.name = name } public int getAge () {return age;} public void setAge (int age) {this.age = age;} @ Override public String toString () {return "UserInfo {" + "name='" + name + ", age=" + age +'}' } / * Builder inner class * / public static class UserInfoBuilder {private String name; private int age; public UserInfoBuilder name (String name) {this.name = name; return this;} public UserInfoBuilder age (int age) {this.age = age; return this } / * create a new instance object and assign * * @ return * / public UserInfo build () {UserInfo userInfo = new UserInfo (); userInfo.setName (this.name); userInfo.setAge (this.age); return userInfo;}
We define a UserInfo class with two attributes of name,age and a static inner class UserInfoBuilder to help us build UserInfo and provide chained methods.
2. Implementation method 1. Introduce dependency org.projectlombok lombok true
Lombok needs to install plug-ins in IDEA. For specific methods, please ask Baidu.
2. Implement package com.asurplus.common.builder.style2;import lombok.Builder;import lombok.Data;/** * get set toString method annotation * / @ Data/** * builder pattern annotation * / @ Builderpublic class UserInfo2 {private String name; private int age;}
Based on the Lombok comments, we omitted most of the code, but the specific implementation is exactly the same as ours, and the invocation method is the same.
3. Test package com.asurplus.common.builder;import com.asurplus.common.builder.style1.UserInfo;import com.asurplus.common.builder.style2.UserInfo2;/** * builder mode * / public class TestMain {public static void main (String [] args) {/ / handwritten UserInfo userInfo = UserInfo.builder (). Name ("Zhang San"). Age (20). Build (); System.out.println (userInfo) / / UserInfo2 userInfo2 = UserInfo2.builder (). Name (Li Si). Age (30). Build (); System.out.println (userInfo2);}}
Output result
These are all the contents of the article "case Analysis of Java Builder pattern". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.