Builder Pattern(译)
Builder pattern vs Constructor vs Setter
You could have built an object with this:
// Example 1
new FlyingMachine("Boeing 787", 2, false);
Or this:
// Example 2
FlyingMachine plane = new FlyingMachine();
plane.setName("Boeing 787");
plane.setNumEngine("Boeing 787");
plane.setHasRocketBooster(false);
Example one (Constructor based instantiation)
In example one, the object is created through constructor. There are multiple downsides with this
The arguments passing into the constructor are not meaningful
What the hell does 2
and false
exactly mean?
In comparison the builder pattern has done a great job explaining the values .numEngine(2)
and .hasRocketBooster(false)
The arguments are all passed in one line making it nearly impossible to read
You may arrange like this
new FlyingMachine( "Boeing 787", 2, false);
But I doubt it really improves readability
You cannot omit parameters
When one day you realize that you do not want to name that flying machine, you may choose to pass in null
for name
new FlyingMachine(null, 2, false);
But using the builder pattern, you can simply remove this line: .name("Boeing 787")
In example two, the internal properties of the object are set by setters after instantiation. It essentially achieves the same effects as the two other methods, but it has a completely different semantic meanings. This method creates an empty object and fills in the details later, which bypasses the constructor completely and these properties cannot be used during initialization. In reality, you won’t build an aircraft without knowing what’s inside, right?
摘录自 effective java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | public class DoDoContact { private final int age; private final int safeID; private final String name; private final String address; public static class Builder { private int age = 0 ; private int safeID = 0 ; private String name = null ; private String address = null ; // name必要 public Builder(String name) { this .name = name; } public Builder age( int val) { age = val; return this ; } public Builder safeID( int val) { safeID = val; return this ; } public Builder address(String val) { address = val; return this ; } public DoDoContact build() { // 通过builder构建实例 return new DoDoContact( this ); } } private DoDoContact(Builder b) { age = b.age; safeID = b.safeID; name = b.name; address = b.address; } } |
1 2 3 4 | DoDoContact ddc = new DoDoContact.Builder( "Ace" ).age( 10 ) .address( "beijing" ).build(); System.out.println( "name=" + ddc.getName() + "age =" + ddc.getAge() + "address" + ddc.getAddress()); |
reference:
https://www.cnblogs.com/moonz-wu/archive/2011/01/11/1932473.html
https://hermannyung.com/2017/05/28/builder-pattern-vs-constructor-vs-setter/
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· JDK 24 发布,新特性解读!
· C# 中比较实用的关键字,基础高频面试题!
· .NET 10 Preview 2 增强了 Blazor 和.NET MAUI
· Ollama系列05:Ollama API 使用指南
· 为什么AI教师难以实现