Builder pattern vs. config object

后端 未结 9 582
执笔经年
执笔经年 2021-01-31 08:40

The builder pattern is popular to create immutable objects, but there is some programming overhead to create a builder. So I wonder why not simply using a config object.

9条回答
  •  南旧
    南旧 (楼主)
    2021-01-31 09:44

    IMO, the builder pattern is much more roboust if you have things like validation, etc.

    The builder pattern in your case can be changed to do the following:

    Product p = new ProductBuilder("pName").alcohol(0.38).size(0.7).price(17.99).build();
    

    The build() method can do all the validation stuff that is needed for your builder. The builder pattern also has several design advangates (all of which may not be applicable in your case). For deatils check this question

提交回复
热议问题