views:

159

answers:

2

I'm working in AS3, but I guess this could be a general question, so I'll frame it more vaguely...

I'm using an XML file to define parameters for a particular class of object that implements the Strategy Pattern. There will be large variety of objects, and this works for us as a designer friendly solution to customizing these objects. As I can only define strings in the XML file, can someone suggest a tidy way to get take that string and implement the proper strategy?

I had two initial thoughts. Firstly, to pass the string to the constructor of the object and then have a Switch Case within the object that applies the correct strategy.

Secondly, so put the switch outside of the class in the controller that then passes the correct strategy to the object's constructor.

The second seems like the cleaner version as then the object class itself isn't affected by my specific implementation. But neither feels quite right.

Any additional suggestions would be appreciated!

+2  A: 

You want some kind of Factory design pattern, to pick the appropriate class based on an input string. A nice application of that is Registry, where applicable classes "register" themselves with a singleton Registry class as being appropriate for a certain input string; the factory itself then just becomes a lookup from string to registered class. Some fallback needs to be present when no class has registered for the input string that's being requested: either that's an exception, or there's a "default" implementation that gets used when no more specific one applies -- the latter's a nice fail-soft option, but not always applicable.

An inevitable issue with Registry is, how to locate all classes of interest and make sure they register themselves (that's particularly of interesting for Plugin classes); the best solutions to this are rather dependent on the specific language at hand and unfortunately I'm not sure what AS3 delivers in this regard (or if you're at all interested in such dynamic expandability).

Another interesting issue with Registry is "conflicts" -- what happens if more than one applicable class claims to be the right one for a certain input string. "Last to claim wins" is simplest, but often too-simple; you may have each class register for multiple input strings with a certain "precedence" or "priority", or else (not really interesting on systems that aren't distributed, but crucial for distributed systems...) "load balance" among all applicable "servers" (round-robin or in more sophisticated ways).

You'll note that these solutions resemble your second one more than your first one, but the key difference is that instead of a brittle, rigid hard-coded switch (an OOP bete noire;-) they rely on flexible mappings from string to class (or class-constructing method, delegate, &c, depending on details of implementation language -- e.g. in Java you might have a StrategyConstructing interface, each strategy class would then register its own auxiliary class implementing that strategy when it signs up to the Registry).

Alex Martelli
A: 

yes, alex pointed out the right way ...

for implementation details, you may want to have a look at this post, which deals with a similar problem ... my answer contains an example implementation to get started with ...

i think, you should do a string <-> instance mapping, since this is more robust ... the strategy should be stateless from the perspective of the applying object, receiving all input from that object ... that way you are able to switch strategies at runtime, since it is not obvious to synchronize states of the object and the strategy at arbitrary points of execution ...

back2dos