design-patterns - 为什么构建器模式的构建器接口(interface)中没有方法 `GetResult()`?

标签 design-patterns builder gang-of-four

来自四人帮的设计模式, enter image description here

示例:

enter image description here

为什么接口(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/

相关文章:

java - 哪种构建器方法更可取?

javascript - JavaScript 中的单例模式

ios - 四种设计模式组合——Objective C 示例代码?

java - 具有静态内部类和双重检查锁定的单例

两个类共享相同和相似但不同方法的 Java 设计模式

shell - help - sed - 在任何 XxxXxx 形式的字符串之间插入空格,而不替换模式

design-patterns - 立面图案与其他图案的区别

java - 构造不可变对象(immutable对象)的抽象生成器

java - 具有静态内部类的构建器模式与具有一个抽象和一个+具体实现的构建器设计模式有什么区别

java - 什么是四人帮设计模式