作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在之前的 post 中已经建议过那StringBuilder是Builder设计模式的一个例子,不过它和四人帮描述的版本有很大不同,看起来他们描述的设计模式更像是下面这样: http://www.oodesign.com/builder-pattern.html
这都是构建器设计模式的示例吗?如果是,它们之间的共同点是什么?
还有,导演戴上钻石的目的是什么?这是否意味着一个或多个混凝土 builder 的组合?
最佳答案
实现可能会有所不同,但总的来说,它们遵循相同的方法。
正如您所知,Builder
模式可以与药物配方并列 - 例如,医生可以在单个配方中添加越来越多的药物处方,并且最后,准备好后,给你完整的食谱。
当使用 Builder 模式构建对象时,我们会执行类似的操作 - 调用方法,这些方法充当如何构建对象的说明,当我们准备好时,我们会使用终端方法获取对象,例如调用 构建()
。
Builder 实现的另一个示例是 java.util.stream.Stream
类,它是 Java 8 中 Stream API 的一部分。
它的工作方式非常相似 - 您可以在 Stream
上调用尽可能多的非终端操作,并最终获得构建 Stream
,基于提供的条件列表。例如:
List<String> names = Arrays.asList("John", "Peter", "Stephen");
//Let's build a Stream, which will give a list of the sizes of each of our names
List<Integer> namesSizes = names
.stream()
.map(name -> name.length())
.collect(Collectors.toList()); //terminal operation
更多信息:
关于java - 生成器模式: Why Are Many Example Implementations different From the GOF Diagram?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26931651/
我是一名优秀的程序员,十分优秀!