Monday, March 12, 2007 05:38 PM
I assume that you are asking how you can arrange for your objects to be arranged into a graph rather than the tree to which the Composite pattern normally refers.
In principle, this is easily achieved since there is no reason why a parent cannot contain the same children as other parents. There is nothing in the Composite Pattern that requires children to know their parents so, as far as the pattern goes, you do not have to worry about that relationship. However, If your particular problem requires your children to remember their parents, then you may need each node to store a collection of its parents. This will bring about issues of creating and maintaining bidirectional relationships, which are nothing to do with the Composite Pattern. If you're having difficulties here, you may want to look at the Mediator Pattern.
Another issue you will run into if you generalise the pattern to a graph is that messages may be sent many times to the same node, once for each path through the graph from the root to the node. If this is the problem that you're having, you might want to look at the Command Pattern - you can make commands remember which node they have applied themselves to.
Hope this helps.