哪种设计模式最合适?
我想创建一个可以使用四种算法之一的类(并且要使用的算法仅在运行时已知)。我认为策略设计模式听起来很合适,但我的问题是每种算法需要的参数略有不同。使用策略,但将相关参数传入构造函数中会是一个糟糕的设计吗?
这是一个例子(为了简单起见,假设只有两种可能的算法)......
class Foo
{
private:
// At run-time the correct algorithm is used, e.g. a = new Algorithm1(1);
AlgorithmInterface* a;
};
class AlgorithmInterface
{
public:
virtual void DoSomething() = 0;
};
class Algorithm1 : public AlgorithmInterface
{
public:
Algorithm1( int i ) : value(i) {}
virtual void DoSomething(){ // Does something with int value };
int value;
};
class Algorithm2 : public AlgorithmInterface
{
public:
Algorithm2( bool b ) : value(b) {}
virtual void DoSomething(){ // Do something with bool value };
bool value;
};
I want to create a class that can use one of four algorithms (and the algorithm to use is only known at run-time). I was thinking that the Strategy design pattern sounds appropriate, but my problem is that each algorithm requires slightly different parameters. Would it be a bad design to use strategy, but pass in the relevant parameters into the constructor?.
Here is an example (for simplicity, let's say there are only two possible algorithms) ...
class Foo
{
private:
// At run-time the correct algorithm is used, e.g. a = new Algorithm1(1);
AlgorithmInterface* a;
};
class AlgorithmInterface
{
public:
virtual void DoSomething() = 0;
};
class Algorithm1 : public AlgorithmInterface
{
public:
Algorithm1( int i ) : value(i) {}
virtual void DoSomething(){ // Does something with int value };
int value;
};
class Algorithm2 : public AlgorithmInterface
{
public:
Algorithm2( bool b ) : value(b) {}
virtual void DoSomething(){ // Do something with bool value };
bool value;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这将是一个有效的设计,因为策略模式要求定义一个接口,并且任何实现该接口的类都是运行策略代码的有效候选者,无论它是如何构造的。
It would be a valid design because the Strategy pattern asks for an interface to be defined and any class that implements it is a valid candidate to run the strategy code, regardless how it is constructed.
我认为这是正确的,如果您拥有创建新策略时所需的所有参数,并且每个阅读代码的人都清楚您所做的事情。
I think it's correct, if you have all the parameters you need when you create the new strategy and what you do is clear for everyone reading the code.
您采用这种方法是正确的。是的,这就是策略模式的本质...“独立于实现改变算法。”您可以给自己一个通用构造函数来传递初始化类所需的参数,例如作为对象数组。
享受!
You are right on with this approach. Yes this is the essence of the strategy pattern..."Vary the algorithm independent of the implementation." You can just give yourself a generic constructor to pass in the parameters you need to initialize your class, such as an object array.
Enjoy!
当您想要在运行时决定使用哪种算法时,策略模式非常有用。
Strategy pattern are useful when you want to decide on runtime which algorithm to be used.
您还可以使用包含键值对的内存块的单个接口传递参数。这样,接口在任何当前和未来的算法之间都是通用的。每个算法实现都知道如何将键值对解码为其参数。
You could also pass parameters in using a single interface of a memory block containing key-value pairs. That way the interface is common between any present and future algorithms. Each algorithm implementation would know how to decode the key-value pairs into its parameters.
恕我直言,您面临着挑战,因为您在具体算法的创建方面和算法的实际运行之间感到困惑。只要
'DoSomething'
接口保持不变,就可以使用策略模式
。只是根据您的情况创建不同的具体算法,可以通过工厂方法设计模式来处理。IMHO, you are facing the challenge as you are confusing between the creational aspect of the concrete algorithm and the actual running of the algorithm. As long as the
'DoSomething'
interface remains the same,Strategy Pattern
can be used. It is only the creation of the different concrete algorithm that varies in your case, which can be handled through aFactory Method
design pattern.