views:

222

answers:

6

Hello,

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;   
};
+2  A: 

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.

Nikko
A: 

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.

Amardeep
Wouldn't this kind of break the whole idea of the strategy pattern since the code calling the strategy would need to know which strategy was being used in order to know which key/value pairs to pass?
Eric Petroelje
No, this would just affect the implementation of the Strategy class. Instead of constructing the algorithms with unique interfaces it would be a uniform one. Application interface shouldn't change. There is perhaps a high cost to do it for a small number of algorithms, but if the number could grow to dozens or more it may be simpler to maintain.
Amardeep
@Eric, I guess that would depend on whether you expect the clients to always supply the same key->value pairs. Of course, that would beg the question why the need for key->value pairs.
Noah Roberts
+6  A: 

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.

Otávio Décio
+2  A: 

Hi,

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!

Doug
+1  A: 

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

priyanka.sarkar_2
A: 

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.

Chubsdad