简体   繁体   English

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

[英]How to implement isGeneralizationOf with the composite specification pattern?

I am trying to implement the composite specification pattern, as per the Specifications Document by Fowler and Evans. 我正在尝试根据Fowler和Evans的“ 规范文档”来实现复合规范模式。

At first impression, I thought the implementation of isGeneralizationOf would be different for conjunction and disjunction. isGeneralizationOf ,我认为isGeneralizationOf的实现对于合取和isGeneralizationOf会有所不同。

In particular, I thought the logic for conjunction would be 我特别认为合取的逻辑是

(1) Let specX be the conjunction of specA and specB. (1)设specX为specA和specB的结合。 Then, specX is a generalization of specC only if both specA and specB are a generalization of specC. 然后,只有specA和specB都是specC的概括,specX才是specC的概括。

And I thought the logic for disjunction would be 我认为分离的逻辑是

(2) Let specY be the disjunction of specA and specB. (2)设specY为specA和specB的和。 Then, specY is a generalization of specC if either specA or specB is a generalization of specC. 然后,如果specA或specB是specC的一般化,则specY是specC的一般化。

However, on page 16 of the document , they show this method: 但是,在文档的第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

Is my reasoning in (1) and (2) correct? 我在(1)和(2)中的推理正确吗? 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? 他们的意图是什么?

CompositeSpecification >> isGeneralizationOf: aSpecification
^aSpecification isSpecializationOf: self

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

#includesAllOf: is defined in the class Collection #includesAllOf:在类Collection中定义

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). 如果{}是动态数组文字的特殊语法(在Array类中定义isSpecializationOf :),则可以在Squeak中获得很好的语法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM