策略模式中的 String 到 Stategy
我正在使用 AS3,但我想这可能是一个普遍问题,所以我将更模糊地描述它......
我使用 XML 文件来定义实现策略模式的特定对象类的参数。将会有各种各样的对象,这对我们来说是一个设计者友好的解决方案来定制这些对象。由于我只能在 XML 文件中定义字符串,有人可以建议一种简洁的方法来获取该字符串并实施正确的策略吗?
我最初有两个想法。首先,将字符串传递给对象的构造函数,然后在对象内有一个应用正确策略的 Switch Case。
其次,将开关放在控制器中的类之外,然后将正确的策略传递给对象的构造函数。
第二个似乎是更干净的版本,因为对象类本身不受我的具体实现的影响。但两者都感觉不太对劲。
任何额外的建议将不胜感激!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要某种工厂设计模式,以根据输入字符串选择适当的类。一个很好的应用是Registry,其中适用的类使用单例Registry类“注册”自己以适合特定的输入字符串;然后工厂本身就变成了从字符串到注册类的查找。当没有类为所请求的输入字符串注册时,需要存在一些后备:要么这是一个例外,要么有一个“默认”实现,当没有更具体的应用时使用 - 后者是一个很好的故障软选项,但并不总是适用。
注册表的一个不可避免的问题是,如何找到所有感兴趣的类并确保它们自行注册(这对于插件类尤其有趣);对此的最佳解决方案相当依赖于手头的特定语言,不幸的是我不确定 AS3 在这方面提供了什么(或者您是否对这种动态可扩展性感兴趣)。
注册表的另一个有趣的问题是“冲突”——如果多个适用的类声称对于某个输入字符串是正确的,会发生什么情况。 “最后宣布获胜”是最简单的,但往往过于简单;您可以让每个类注册具有特定“优先级”或“优先级”的多个输入字符串,或者(对于非分布式系统来说并不是很有趣,但对于分布式系统至关重要......)所有之间的“负载平衡”适用的“服务器”(循环或更复杂的方式)。
您会注意到,这些解决方案与您的第二个解决方案比您的第一个解决方案更相似,但主要区别在于,它们依赖于从字符串到类的灵活映射,而不是脆弱的、严格的硬编码开关(OOP bete noire;-) (或类构造方法、委托、&c,取决于实现语言的细节——例如,在 Java 中,您可能有一个 StrategyConstructing 接口,每个策略类在注册到注册表)。
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).
是的,亚历克斯指出了正确的方法......
有关实现细节,您可能需要查看 这篇文章,处理类似的问题...我的答案包含一个示例实现来开始...
我认为,你应该做一个字符串 < ;->实例映射,因为这更稳健...从应用对象的角度来看,该策略应该是无状态的,接收来自该对象的所有输入...这样您就可以在运行时切换策略,因为这并不明显在任意执行点同步对象和策略的状态...
greetz
back2dos
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 ...
greetz
back2dos