由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - about builder pattern
相关主题
SCJP都考些啥?String[] a = c.toArray(new String[0])
出个简单题,看你Java APi熟悉到什么程度Generics应该怎么得到它的class?
有多少人对annotation这个东西不满,请举手!!请教Programming books
一个Java程序员的话(3)请问java的ide
JAVA里什么METHOD是用来STRING PATTERN SEARCHany tools for ...
java regex pattern question使用eclipse方法对第三方库建立Wrapper的小技巧。
求教:Facade Pattern vs Static Variablehow to refactor overlayered applications?
why doesn't replaceAll work?Any body uses wicket framework for web development?
相关话题的讨论汇总
话题: builder话题: string话题: product话题: pattern话题: final
进入Java版参与讨论
1 (共1页)
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
6
oh, after seeing you mention joshua, I google out this:
http://rwhansen.blogspot.com/2007/07/theres-builder-pattern-that-joshua.html
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
1 (共1页)
进入Java版参与讨论
相关主题
Any body uses wicket framework for web development?JAVA里什么METHOD是用来STRING PATTERN SEARCH
Core J2EE Design Patternjava regex pattern question
求一本关于Design Pattern,UML和JAVA的经典书籍求教:Facade Pattern vs Static Variable
Java/J2EE的书why doesn't replaceAll work?
SCJP都考些啥?String[] a = c.toArray(new String[0])
出个简单题,看你Java APi熟悉到什么程度Generics应该怎么得到它的class?
有多少人对annotation这个东西不满,请举手!!请教Programming books
一个Java程序员的话(3)请问java的ide
相关话题的讨论汇总
话题: builder话题: string话题: product话题: pattern话题: final