> Chain of responsibility is another option for creating them, although if the
> strategy objects are completely different, can be added or removed
> dynamically, or the relationship between the algorithm and the key isn't
> fixed then the factory is generally a better option as it removes the
> coupling between all the strategies themselves.

It seems to me that CoR adds little value to the original question if I can
remember it correctly ;-) The fact that it's a chain is not important because
it's unlikely that the list of known processors for the various flavors of the
document are unknown. Second, this discussion of the CoR came about because of
desire to not have a switch statement to create the strategies.

First the use of switch statment is not evil unless then list of options is
long (I use 7-10 as a rule of thumb) and the list of options is pretty static.
But if you want something more elegant how about this approach. Assume that the
strategy pattern was used to describe the various vocabulary processors and
that you have a list of strategies additionally described by interface
VocabStrategy:


public VocabStrategy getStrategy(string vocabularyType)
{
  VocabStrategy[] processors = getStrategies(); //processors could be static if
that's ok
  foreach (VocabStrategy strategy in processors)
    if (strategy.CanHandle(vocabularyType) return strategy;
}


foreach (XmlNode blah......)
{
  VocabStrategy strategy = getStrategy(blah.<attribute or text containing
type>);
  strategy.Process(blah.<get the correct nodes somehow>
}

This seems to be far less code than setting up either the strategies to know
about the chain and having to delegate to the next and works perfectly, with
all the normal provisos about not have actually coded this ;-)


Philip - http://blogs.xcskiwinn.org/panmanphil
"There's a difference between righteous anger and just being crabby" - Barbara

===================================
This list is hosted by DevelopMentor�  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to