概述
In classes that have a lot of fields you oftentimes end up with many constructors as you might need objects using different field combinations. The Builder pattern enables a more readable object creation and let’s you specify the fields that are actually needed.
建造者模式就是有一个类(Builder)专门帮助你方便地创建另一个类(JavaBean)的实例。
这种做法好处主要在于可以方便的创建对象。
菜鸟教程给出下列的好用处,但是我很少用到。
![](https://img.haomeiwen.com/i21501654/a64d0eea206557e2.png)
值得一提的是 JavaBean应用Builder模式可以更方便创建对象。
![](https://img.haomeiwen.com/i21501654/8b77a55e71fe28fa.png)
![](https://img.haomeiwen.com/i21501654/3f9fa49d4b2147a9.png)
![](https://img.haomeiwen.com/i21501654/aa846d161249dbed.png)
![](https://img.haomeiwen.com/i21501654/5ad8aaef8b8dd92c.png)
一般都会有javabean Builder的工具,推荐Idea的Code Genarator工具。参考了这篇文章。
网友评论