1

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?

Ed I
  • 7,008
  • 3
  • 41
  • 50

2 Answers2

0
CompositeSpecification >> isGeneralizationOf: aSpecification
^aSpecification isSpecializationOf: self

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

#includesAllOf: is defined in the class Collection

Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66
  • Thanks, but that doesn't seem right to me yet. Suppose you let specX be (specA OR specB). Also, suppose you let specY be (specA AND specB). According to your definition of isSpecializationOf, specX is a specialization of specY, since the components of specX include all of the components of specY. However, (specA OR specB) is not a special case of (specA AND specB). More to the point, does the method by Fowler and Evans make sense to you? – Ed I Feb 15 '11 at 21:57
  • The trick is in where you do the logical combinations. You can model both AND and OR with just sets: if you want to test AND you check inside the statement, if you want to test OR you check in two separate statements and then combine them using or (Smalltalk "|"). – Bernd Elkemann Feb 21 '11 at 22:24
  • I will add an example as another answer, because i cannot use multiple lines in a comment – Bernd Elkemann Feb 21 '11 at 22:32
0

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).

Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66