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

发布于 2024-10-16 18:47:36 字数 788 浏览 1 评论 0原文

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

乍一看,我认为 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)中的推理正确吗?如果是错的,那是为什么呢?如果它是正确的,那么为什么作者定义一个由合取和析取规范继承的单一方法?他们来这里的目的是什么?

I am trying to implement the composite specification pattern, as per the Specifications Document by Fowler and Evans.

At first impression, I thought the implementation of isGeneralizationOf would be different for conjunction and disjunction.

In particular, I thought the logic for conjunction would be

(1) Let specX be the conjunction of specA and specB. Then, specX is a generalization of specC only if both specA and specB are a generalization of specC.

And I thought the logic for disjunction would be

(2) Let specY be the disjunction of specA and specB. Then, specY is a generalization of specC if either specA or specB is a generalization of specC.

However, on page 16 of the document , they show this method:

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

Is my reasoning in (1) and (2) correct? If it's wrong, why is that? If it's correct, then why did the authors define a single method to be inherited by both conjunction and disjunction specifications? What is their intent here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有影子陪我不离不弃 2024-10-23 18:47:36

示例:

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:)。

Examples:

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

You can get the syntax this nice in Squeak if you first define the objects a and b, since {} is a special syntax for dynamic array literals (define isSpecializationOf: in class Array).

带刺的爱情 2024-10-23 18:47:36
CompositeSpecification >> isGeneralizationOf: aSpecification
^aSpecification isSpecializationOf: self

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

#includesAllOf:在类 Collection 中定义

CompositeSpecification >> isGeneralizationOf: aSpecification
^aSpecification isSpecializationOf: self

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

#includesAllOf: is defined in the class Collection

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文