如何使用复合规范模式实现isGeneralizationOf?

时间:2011-02-10 22:37:06

标签: design-patterns domain-driven-design smalltalk specification-pattern

我正在尝试按照Fowler和Evans的Specifications Document来实现复合规范模式。

在第一印象中,我认为isGeneralizationOf的实现对于连接和分离会有所不同。

特别是,我认为连接的逻辑是

(1)让specX成为specA和specB的结合。然后,只有当specA和specB都是specC的泛化时,specX才是specC的推广。

我认为分离的逻辑是

(2)让specY成为specA和specB的分离。那么,如果specA或specB是specC的泛化,specY是specC的推广。

但是,在该文档的第16页上,他们显示了这种方法:

CompositeSpecification >> isGeneralizationOf: aSpecification
"True if each component is subsumed. False if any component is not subsumed."
^ (self components contains:
        [:each |(each isGeneralizationOf: aSpecification) not ]) not

我在(1)和(2)中的推理是否正确?如果这是错的,为什么呢?如果它是正确的,那么为什么作者定义了一个单独的方法来继承连接和析取规范?他们的意图是什么?

2 个答案:

答案 0 :(得分:0)

CompositeSpecification >> isGeneralizationOf: aSpecification
^aSpecification isSpecializationOf: self

CompositeSpecification >> isSpecializationOf: aSpecification
^self components includesAllOf: aSpecification

#includesAllOf:在Collection

类中定义

答案 1 :(得分:0)

示例:

The following models: the spec "a AND b" is specialization of "a OR b"

({a,b} isSpecializationOf: {a}) & ({a,b} isSpecializationOf: {b})
-> true

This following models: the spec "a OR b" is specialization of "a AND b"

({a} isSpecializationOf: {a,b}) | ({b} isSpecializationOf: {a,b})
-> false

如果你首先定义对象a和b,你可以在Squeak中获得这个很好的语法,因为{}是动态数组文字的特殊语法(在类Array中定义isSpecializationOf:)。