|
This Pattern can be used to follow a relation and call a method on all connected objects. Depending on the relations cardinality different code is generated.
Former Patterns: SingleIndirection (obsolete) , ComplexIndirection (obsolete) , ExtendedComplexIndirection (obsolete) .
FollowRelation can be used in every case, when relations schould be traversed and an operation schould be called on all connected objects. For example, to print all connected objects a remote operation print could be called on each connected object. The Pattern can be used to follow several relations with any cardinalities.
To use this Pattern, only a target class and the rolenames of the relations that should be followed has to be known.
bind: 'target' to: 'Room'; bind: 'operation' to: 'printAll'; bind: 'remoteOperation' to: 'print'; bind: 'relation' to: '#(#LeftWalls #controlOfRoom)'.
This example will generate a method printAll which will call a print method on all connected LeftWalls and Controls. The Pattern takes care of the cardinality of the relations. The generated method in Smalltalk might look like this:
"follow all relations." "1:n relation." self LeftWalls connections do: [ :anObject| anObject print ]. "1:1 relation." self controlOfRoom instance notNil ifTrue: [ self controlOfRoom instance print ].
"follow all relations" {loop}{optionalParameter}
For each relation one loop macro will be evaluated. These loop macros are replaced by a macro called {doLoop} if the actual relation is of cardinality 1:n and by {dontLoop} for cardinalities 1:1.
"1:1 relation." self {relation} instance notNil ifTrue: [ self {relation} instance {remoteOperation}{optionalParameter} ].
"1:n relation." self {relation} connections do: [ :anObject| anObject {remoteOperation}{optionalParameter} ].
If {parameter} is set, {optionalParameter} will be replaced with ': {parameter}', else it is replaced by an empty string.
Pattern FollowRelation Category indirection ObjectType use target SingleMethod implement operation ComplexRelation use relation Expression implement optional remoteOperation Expression implement optional parameter End