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

How to use the builder mode

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to use the builder model". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1 use the scene

The Builder pattern is often seen in the process of reading the source code, mainly to simplify the creation of complex objects.

The specific examples of those houses will not be mentioned, and they will mainly be used in practical work.

@ Builder comment

If your partner uses the lombok framework, you will be familiar with the @ Builder annotation.

@ Data @ Builder public class UserRespVo {/ * * user name * / private String userName; / * * user id * / private String userId Public static void main (String [] args) {UserRespVo respVo = UserRespVo.builder () .UserID ("liuzhihang") .username ("programmer") .build ();}}

Take a look at the compiled .class file.

Public class UserRespVo {private String userName; private String userId; UserRespVo (final String userName, final String userId) {this.userName = userName; this.userId = userId;} public static UserRespVo.UserRespVoBuilder builder () {return new UserRespVo.UserRespVoBuilder ();} public static class UserRespVoBuilder {private String userName; private String userId UserRespVoBuilder () {} public UserRespVo.UserRespVoBuilder userName (final String userName) {this.userName = userName; return this;} public UserRespVo.UserRespVoBuilder userId (final String userId) {this.userId = userId; return this } public UserRespVo build () {return new UserRespVo (this.userName, this.userId);}

You create a static inner class of Builder internally, as well as a builder () method. This allows you to make chained calls.

If the creation of complex objects, partners can also adopt the lombok approach, first create builder objects, and then build the object step by step.

In the source code and work

When reading source code or using open source software, you often encounter builder patterns, such as OkHttp3Utils

Private static OkHttpClient client = new OkHttpClient .Builder () .readTimeout (60, TimeUnit.SECONDS) .connectTimeout (60, TimeUnit.SECONDS) .writeTimeout (120, TimeUnit.SECONDS) .build ()

For example, the construction of InstanceInfo in the Eureka source code.

InstanceInfo.Builder

For example, the code that uses Elasticsearch to create client

RestClient restClient = RestClient.builder (new HttpHost ("localhost", 9200, "http"), new HttpHost ("localhost", 9201, "http") .build ()

Also, such as Mybatis Plus, etc.

This is the end of "how to use the Builder Mode". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report