Forums Register Login

Searching/Traversing Composites

+Pie Number of slices to send: Send
Hello all -

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.

Thanks!
R. Alcazar
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Stan -

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.

Kind Regards,
R. Alcazar
+Pie Number of slices to send: Send
 

it was getting very messy with switch/case statements



This really is the sign that strategy (or State or Command or one of those structurally similar patterns) might be a candidate. Make it work this time and you'll never miss that sign again.
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 823 times.
Similar Threads
Continents and Oceans
Is J2ME going to be big?
Getting the location of a user.
My Small Web Project - but has some problems - please help ...
Appreciation for American Culture
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:43:42.