Why does the builder pattern not have a method `GetResult()` in the builder interface?

吃可爱长大的小学妹 提交于 2019-12-13 01:45:51

问题


From Design Pattern by Gang of Four,

Example:

Why doesn't the interface Builder have a method GetResult(), which is overridden in the concrete class ConcreteBuilder?

In the example, the concrete builders have GetXXX() methods, where XXX is different for different concrete builders, which doesn't promote "programming to interface" at all.

Is this "omission" deliberate in the builder pattern?


回答1:


Yes, the omission is deliberate. The book addresses it directly.

Why no abstract class for products? In the common case, the products produced by the concrete builders differ so greatly in their representation that there is little to gain from giving different products a common parent class. Because the client usually configures the director with the proper concrete builder, the client is in a position to know which concrete subclass of Builder is in use and can handle its products accordingly.

Regarding the comments to the OP, generics could assist the client in handling products by enabling a GetResult() method with a generic return type in the Builder interface.



来源:https://stackoverflow.com/questions/46495698/why-does-the-builder-pattern-not-have-a-method-getresult-in-the-builder-inte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!