我正在寻找一种设计模式,它能够基于应用行为的抽象主题的实现来实现函数的动态行为。
一个抽象的例子:
interface IPlace
{
}
class Garage implements IPlace
{
}
class Backyard implements IPlace
{
}
class Mover
{
void Move(IPlace source, IPlace destination);
}使用示例:
void NotMain()
{
IPlace garage = new Garage();
IPlace backyard = new Backyard();
Mover mover = new Mover();
mover.move(garage, garage);
mover.move(garage, backyard);
mover.move(backyard, garage);
mover.move(backyard, backyard);
}预期结果:
Mover moved an item inside the garage
Mover moved an item from the garage to the backyard
Mover moved an item from the backyard to the garage because it's raining
Mover moved an item around in the backyard我认为答案可能是策略模式,但我不确定应该如何决定行为,因为有n^2种不同的行为,其中n是IPlace接口的实现数量。也许是一个返回MoveOperation对象的工厂?
我对这类问题的最佳实践解决方案很感兴趣。
发布于 2017-09-13 09:11:34
这似乎不是策略设计模式的情况。这是一个简单的继承。以'I‘(IPlace)开头的接口名称没有任何作用,因为它也像实现中的任何其他’类型‘一样。
在以下情况下应用策略模式
,
,
,
在上面的问题中,所有这些都丢失了。此外,Place可以是Abstract类,而不是Interface,这样您就可以推送与所有Place相关的公共状态。
https://stackoverflow.com/questions/46183999
复制相似问题