I've created an object hierarchy (Area <- AreaCollection) using the Composite Pattern (GoF). The objects in my hierarchy represent geographical regions illustrated below:
World - Asia - Europe - North America - Canada - United States
and so on...
Each area can have a set of users associated with them such that:
World - Asia (J. Smith) - Europe - North America (M. Smith) - Canada - United States (T. Reilly)
I'd like to invoke a method defined in the Composite class that returns the user associated with that object. However, if there is no user defined for that object, the method will recursively traverse up the tree until a user is found. I have created an implementation that allows me to traverse upward until a user is found, but I'd like a little more granular control, similar to the LDAP class SearchControls OBJECT_SCOPE, ONELEVEL_SCOPE, SUBTREE_SCOPE.
I have given a lot of thought of how to accomplish this and tried to find resources on the web that gives an example of how to do this, however, I can't think of a way to do this.
Can anyone provide me with some ideas or direction on how to accomplish this? Any advice you can offer is appreciated.
See if the Strategy pattern fits your problem. Put your current tree searching code into a class of its own. Then "extract interface" by figuring out which methods absolutely have to be there for a searcher to work. Then you can build an implementation for each algorithm you'd like to use. Here's a very rough idea of how it might look:
Does that seem to be going an interesting direction? [ June 24, 2006: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Thanks for giving me some guidance, I have not yet considered using the Strategy to implement different traversing behavior. My implementation was handling all 3 types of searches in a single method, it was getting very messy with switch/case statements.
Again, thanks for the tip, I'll let you know how it turns out when I have a chance to experiment tomorrow.