当前位置:网站首页>How to make good use of builder mode
How to make good use of builder mode
2022-08-04 20:25:00 【51CTO】
介绍
I wrote a project some time ago,An attribute of a class is called more than one.Just started writing a bunch directlyset代码,后来setThe code is just too much,真心看不下去了,Refactored a bit using the builder pattern,嗯,看起来舒服多了.Basically, the builder pattern is used in many frameworks,每个JavaProgrammers must have used the builder pattern too,You just don't realize that this is the builder pattern.
就像你用Collections.sort(List,Comparator)和Arrays.sort(Object[],Comparator)This is a typical implementation of the strategy pattern,Different effects are achieved by passing in different comparators.
改造过程
First let me demonstrate the process of my transformation,用了Lombok插件,不会的可以百度一下.
原来的实现
测试类
后来的实现
测试类
全程链式调用,Know clearly what each field means,And also save the number of lines,not too cool.
How to write a builder pattern?
Look directly at the final compilation generatedclassThe contents of the decompiled file
I don't think I need to explain what this code means for you,That's two parts.
1.Generate builder class,Assign a value to the builder class
2.Use the builder class to generate the desired class
恭喜你,Already understand what the builder pattern is.
Lombok中@Builder注解的使用
if available in your projectLombok插件,You don't have to write your own builder class,直接用@Builder注解即可.如果不可以,Writing a builder class that mimics the above class is also [email protected] the following points to note.
There is no case of inheritance
上面已经演示了
There are cases of inheritance
1.对于父类,使用@AllArgsConstructor注解
2.对于子类,Manually write full-parameter constructors,Internally calls the parent class full parameter constructor,Used on subclass full-argument [email protected]注解
在框架中的应用
基本上所有的JavaFrameworks use the builder pattern,Because chained assignment is really cool.我GoogleA little bit of code using the builder pattern,方便大家理解
Guava Cache框架
参考博客
[1]https://mp.weixin.qq.com/s/91C9X3Zn6Es66yqTqgnPFA
[2]https://mp.weixin.qq.com/s/8Ults75_X8gyshWCatCzUw
边栏推荐
- 如何推动乡村振兴的落地
- Tear down the underlying mechanism of the five JOINs of SparkSQL
- awk statistical average max min
- 刷题-洛谷-P1319 压缩技术
- How to manually download and install SAP Fiori tools - Extension Pack for Visual Studio Code
- ADB 安装 + 打驱动全教程
- jMeter Thread group 对应的 constant timer
- MySQL字段类型
- 二叉树的遍历
- Zero-knowledge proof notes - private transaction, pederson, interval proof, proof of ownership
猜你喜欢
"WAIC 2022 · hackers marathon" two ants wealth competition invited you to fight!
vscode离线安装插件方法
Ant Group's time series database CeresDB is officially open source
零知识证明笔记——私密交易,pederson,区间证明,所有权证明
Seata source code analysis: various message processing processes of seata server
WIN10系统如何开启终端
二叉树是否对称
C#弹出询问对话框
遇到MapStruct后,再也不手写PO,DTO,VO对象之间的转换了
力扣题(5)—— 最长回文子串
随机推荐
使用 Chrome 开发者工具 coverage 功能分析 web 应用的渲染阻止资源的执行分布情况
【有奖征文】秋招特训,打造你的专属产品体验
漫画 | 老板裁掉我两周后,又把我请回去,工资翻番!
Win10 uwp use ScaleTransform magnify an element
【AGC】构建服务1-云函数示例
「 WAIC 2022 · 黑客马拉松」蚂蚁财富两大赛题邀你来战!
简单理解 JS 事件循环
c语言小项目(三子棋游戏实现)
run command for node
Use "green computing" technology to promote sustainable development of computing power
MySQL字段类型
【Web漏洞探索】跨站脚本漏洞
C#将对象转换为Dictionary字典集合
String中的hashcode缓存以及HashMap中String作key的好处
vehemently condemn
刷题-洛谷-P1179 数字统计
工龄10年的测试员从大厂“裸辞”后...
QT(41)-多线程-QTThread-同步QSemaphore-互斥QMutex
从卖产品到卖“链路”:20条策略 解读直播带货迭代玩法
如何推动乡村振兴的落地