• Post Reply Bookmark Topic Watch Topic
  • New Topic

Searching/Traversing Composites  RSS feed

 
Ricardo Alcazar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ricardo Alcazar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!