[Tutor] Restricting the type of passed-in objects

alan.gauld@bt.com alan.gauld@bt.com
Sun, 13 May 2001 00:21:31 +0100


> First I am going to do it as a class.  A tree would be 
> defined as one or more treenodes.


The classic way to do this as defined by the GoF is to create 
a structure with an abstract superclass (TreeElement say) which 
has 2 subclasses TreeNode, TreeItem say. The first is an empty 
node simply pointing to other TreeElements and the latter is an 
actual data item within the tree.

If they all have the same methods you can treat them all the 
same.
This is described in the Patterms book under the "composite pattern"

If you do that your tree code can be independant of the actual 
data. Of course the consumer of the data still has to worry 
about its type but thats not your tree's worry!


Alan G