示例:
为什么接口(interface) Builder
没有方法 GetResult()
,该方法在具体类 ConcreteBuilder
中被重写?
在示例中,具体构建器具有 GetXXX()
方法,其中 XXX
对于不同的具体构建器是不同的,这不会促进“接口(interface)编程”全部。
构建器模式中的这种“遗漏”是故意的吗?
最佳答案
是的,这个遗漏是故意的。本书直接解决了这个问题。
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.
关于OP的注释,泛型可以通过在Builder
接口(interface)中启用具有泛型返回类型的GetResult()
方法来帮助客户端处理产品。
关于design-patterns - 为什么构建器模式的构建器接口(interface)中没有方法 `GetResult()`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46495698/