简体   繁体   English

这两种方法有什么区别[Smalltalk Best Practice Patterns - Kent Beck]?

[英]What is the difference between those two approaches [Smalltalk Best Practice Patterns - Kent Beck]?

How to convert information from one object's format to another?如何将信息从一个对象的格式转换为另一种?

In Smalltalk best practice patterns by Kent Beck , he discouraged "adding all the possible protocol needed to every object they may be asked of it".Kent BeckSmalltalk 最佳实践模式中,他不鼓励“将所有可能需要的协议添加到每个 object 可能被要求的协议中”。 Instead, he suggested to convert from one object to another.相反,他建议从一个 object 转换为另一个。

Can someone give me an example of what he meant by "overwhelming object's protocol"?有人可以举个例子说明他所说的“压倒性对象的协议”是什么意思吗? I am trying to understand the bad way to do it in order to be able to appreciate the good way.我试图了解这样做的坏方法,以便能够欣赏好的方法。

Reference: Smalltalk by best practice patterns - Page 28参考:Smalltalk by best practice patterns - Page 28

As Beck explains, some clients may need to enumerate a collection in a way that the elements are sorted before exposing them, others would require not iterating twice over the same object (which may appear twice in the collection), etc.正如 Beck 解释的那样,一些客户端可能需要以元素在公开之前对其进行排序的方式枚举集合,其他客户端则不需要在同一个 object 上迭代两次(可能在集合中出现两次),等等。

One way to address these situations would be to add methods such as #sortedDo: , #withoutDuplicatesDo: , etc. to the collection class.解决这些情况的一种方法是将#sortedDo:#withoutDuplicatesDo:等方法添加到集合 class 中。 Sooner or later, this approach would derive in populating the class with other variants of #do: such as #sortedSelect: , #withoutDuplicatesCollect: , and the like.迟早,这种方法会在 class 中使用#do:的其他变体,例如#sortedSelect:#withoutDuplicatesCollect:等。 The problem is that the resulting protocol of the class would quickly grow too large, adding complexity to the simple task of finding the right selector, increasing the risk of duplicating pieces of code when the search is not exhaustive enough, etc.问题是 class 的最终协议会很快变得太大,增加了寻找正确选择器的简单任务的复杂性,增加了在搜索不够详尽时重复代码片段的风险等。

To avoid those side effects, the class should provide methods for converting its instances in instances of other classes.为避免这些副作用,class 应提供在其他类的实例中转换其实例的方法。 So, instead of #sortedDo: the client may use所以,而不是#sortedDo:客户端可以使用

aCollection asSortedCollection do: aBlock

or或者

aCollection asSet do: aBlock

for iterating without duplicates.用于没有重复的迭代。

This explains why we have abundant conversion methods: #asArray , #asOrderedCollection , etc. Note also that conversion methods are not restricted to collections, they are also available for other classes: #asInteger , #asFloat , #asString , #asSymbol , etc. Thanks to them, the services that clients usually need can be obtained by combining a conversion with the appropriate message, without overwhelming the class of the object at hand with all possible combinations, which would multiply (rather than add) all the possibilities.这解释了为什么我们有丰富的转换方法: #asArray#asOrderedCollection等。还要注意,转换方法不仅限于 collections,它们还适用于其他类: #asInteger#asFloat#asString 、# #asSymbol等。多亏了他们,客户通常需要的服务可以通过将转换与适当的消息相结合来获得,而不会压倒手头的 object 的 class 和所有可能的组合,这将增加(而不是增加)所有可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kent Beck的测试驱动开发示例 - Examples in Test Driven Development By Example by Kent Beck Erlang 中的进程/消息和 Smalltalk 中的对象/消息有什么区别? - What is the difference between processes/messages in Erlang and objects/messages in Smalltalk? 适配器和代理模式之间的确切区别是什么? - What is the exact difference between Adapter and Proxy patterns? 'Adapter'和'Mediator'模式之间的确切区别是什么? - What is the exact difference between 'Adapter' and 'Mediator" patterns? 为什么 class Money 通过示例扩展 Kent Beck 的 TDD 中的表达式? - Why does class Money extend Expression in Kent Beck's TDD by Example? 工厂函数和模块模式有什么区别? (更多下文) - What is the difference between factory functions and module patterns? (more below) 这两个构造函数有什么区别? - What is the difference between this two constructor? Smalltalk和Java中的OO有哪些主要区别? - What are the key differences between OO in Smalltalk and Java? 这两种继承有什么区别? - What is the difference between the two types of inheritance? 这两个陈述之间有什么区别 - What's the difference between these two statements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM