4 つのアルゴリズムのいずれかを使用できるクラスを作成したいと考えています (使用するアルゴリズムは実行時にしかわかりません)。Strategy 設計パターンは適切に聞こえると考えていましたが、私の問題は、各アルゴリズムがわずかに異なるパラメーターを必要とすることです。戦略を使用するのは悪い設計ですが、関連するパラメーターをコンストラクターに渡しますか?
以下に例を示します (簡単にするために、考えられるアルゴリズムが 2 つしかないとしましょう) ...
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;
};