哪种设计模式最合适?

发布于 2024-09-04 04:04:06 字数 739 浏览 4 评论 0原文

我想创建一个可以使用四种算法之一的类(并且要使用的算法仅在运行时已知)。我认为策略设计模式听起来很合适,但我的问题是每种算法需要的参数略有不同。使用策略,但将相关参数传入构造函数中会是一个糟糕的设计吗?

这是一个例子(为了简单起见,假设只有两种可能的算法)......

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

纸短情长 2024-09-11 04:04:06

这将是一个有效的设计,因为策略模式要求定义一个接口,并且任何实现该接口的类都是运行策略代码的有效候选者,无论它是如何构造的。

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.

江南烟雨〆相思醉 2024-09-11 04:04:06

我认为这是正确的,如果您拥有创建新策略时所需的所有参数,并且每个阅读代码的人都清楚您所做的事情。

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.

套路撩心 2024-09-11 04:04:06

您采用这种方法是正确的。是的,这就是策略模式的本质...“独立于实现改变算法。”您可以给自己一个通用构造函数来传递初始化类所需的参数,例如作为对象数组。

享受!

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!

像你 2024-09-11 04:04:06

当您想要在运行时决定使用哪种算法时,策略模式非常有用。

Strategy pattern are useful when you want to decide on runtime which algorithm to be used.

调妓 2024-09-11 04:04:06

您还可以使用包含键值对的内存块的单个接口传递参数。这样,接口在任何当前和未来的算法之间都是通用的。每个算法实现都知道如何将键值对解码为其参数。

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.

花开柳相依 2024-09-11 04:04:06

恕我直言,您面临着挑战,因为您在具体算法的创建方面和算法的实际运行之间感到困惑。只要'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 a Factory Method design pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文