m*****k 发帖数: 731 | 1 我们team有一senior programmer写了如同以下
的程序(我简化了非实质内容),他说是用了builder pattern,
我实在不明白这有何好处,各位以为呢?
just becoz we care about "final" then we create a builder which is almost a
duplicate of the original class?
public class Product
{
private final String m_a;
private final String m_b;
private final String m_c;
....
private final String m_z
protected Product(Builder builder)
{
super();
m_a = builder.m_a;
m_b = builder.m_b;
m_c = builder.m_c;
|
h*****0 发帖数: 4889 | 2 我觉得更好看的模式是直接:
Product p = Builder.build(a,b,c,d...);
a
【在 m*****k 的大作中提到】 : 我们team有一senior programmer写了如同以下 : 的程序(我简化了非实质内容),他说是用了builder pattern, : 我实在不明白这有何好处,各位以为呢? : just becoz we care about "final" then we create a builder which is almost a : duplicate of the original class? : public class Product : { : private final String m_a; : private final String m_b; : private final String m_c;
|
g*****g 发帖数: 34805 | 3 Builder pattern is useless until you have 2 builders
to begin with.
a
【在 m*****k 的大作中提到】 : 我们team有一senior programmer写了如同以下 : 的程序(我简化了非实质内容),他说是用了builder pattern, : 我实在不明白这有何好处,各位以为呢? : just becoz we care about "final" then we create a builder which is almost a : duplicate of the original class? : public class Product : { : private final String m_a; : private final String m_b; : private final String m_c;
|
m******t 发帖数: 2416 | 4 The idea behind the builder pattern is
to achieve a more fluid API. It would
make more sense if, e.g., a
Product instance requires a complex
sequence of API calls before it can be
used.
A side note - I'm not sure about
the Product constructor that takes
a Builder instance. I wouldn't have
Product depend on Builder, just like
I wouldn't have any core business object
depend on a helper class.
almost a
【在 m*****k 的大作中提到】 : 我们team有一senior programmer写了如同以下 : 的程序(我简化了非实质内容),他说是用了builder pattern, : 我实在不明白这有何好处,各位以为呢? : just becoz we care about "final" then we create a builder which is almost a : duplicate of the original class? : public class Product : { : private final String m_a; : private final String m_b; : private final String m_c;
|
b******y 发帖数: 1684 | 5 lz帖子里面的这个用法好像是照搬Joshua Bloch那本书里面的
【在 m******t 的大作中提到】 : The idea behind the builder pattern is : to achieve a more fluid API. It would : make more sense if, e.g., a : Product instance requires a complex : sequence of API calls before it can be : used. : A side note - I'm not sure about : the Product constructor that takes : a Builder instance. I wouldn't have : Product depend on Builder, just like
|
m*****k 发帖数: 731 | |
l***i 发帖数: 289 | 7 和这个'pattern'贴些边的是fluent interface http://martinfowler.com/bliki/FluentInterface.html,属于internal domain-specific language。
builder的本质上是把对象一点点'捏'出来
例如:
htmlBuilder.beginParagraph();
htmlBuilder.text("Hey you, refactor to patterns only!");
htmlBuilder.endParagraph();
htmlBuilder.newLine();
Html testimony = htmlBuilder.getHtml();
a
【在 m*****k 的大作中提到】 : 我们team有一senior programmer写了如同以下 : 的程序(我简化了非实质内容),他说是用了builder pattern, : 我实在不明白这有何好处,各位以为呢? : just becoz we care about "final" then we create a builder which is almost a : duplicate of the original class? : public class Product : { : private final String m_a; : private final String m_b; : private final String m_c;
|
s******e 发帖数: 493 | 8 For me, it seems an example of overuse.
Builder pattern is one of 5 creational gof pattern. It will provide you
flexibility to build class based on the components. For me you would like to
choose this pattern if
1. the components can be coming in different fllavors
2. the components share the same interface.
For example, if you want to build a house, you have many kinds of windows to
choose, all these windows share the same interface, but they loos different
, so you can call house.buildWindow(e |