S
syncman
I think there are 2 options for how to implement the Strategy pattern.
One is to use polymorphism; derived classes have the same interface
and can be plugged in. The other is to use templates: Instantiate the
class, passing in a functor (algorithm). Code samples below.
The problem with the first way is that the interface must be the same
(parameters passed in) for all strategies. The problem with the
second way is that there is no common base class, and that means that
I can't put these items on an STL queue (which requires that all its
types be homogeneous).
In my case, I want to make a general Task class, and put them on a
queue. Later, they will be taken off and executed by calling their
function do(). But in cases where you want to pass a few things in,
how do you do it? You could use Windows old trick of passing in 2
general parameters, one being a pointer to whatever you wish. But you
still need type information to interpret it.
Treat this as semi-pseudocode: You don't need this code to answer the
question.
// -------------------------
//First way: polymorphism
class base_strategy (string filename, int offset)
{
string filename, int offset;
base_strategy (string filename, int offset)
: filename(filename),offset(offset) {}
virtual void do() =0;
};
class strategy1 (string filename, int offset)
{
string filename, int offset;
strategy1 (string filename, int offset)
: filename(filename),offset(offset) {}
virtual void do() {//print contents of file starting at offset};
};
// But what if I want a strategy that also inputs another number?
// It's too late to change the interface.
// And isn't it illegal to have a container of pointers? Some STL
rule?
main()
{
queue<base_strategy *> q;
base_strategy *p = new strategy1 ("a.cpp", 234);
q.insert (p);
}
// ------------------------
//Second way: functors (function object)
class strategy1
{
int a,b;
strategy1 (int a, int b) : a(a), b(b) {};
void operator()(int i) {output a*b+2*i;}
}
for_each(q.begin(), q.end(), strategy1(3,4));
// That would work fine, but this doesn't help me with containers.
Sorry for the long message.
One is to use polymorphism; derived classes have the same interface
and can be plugged in. The other is to use templates: Instantiate the
class, passing in a functor (algorithm). Code samples below.
The problem with the first way is that the interface must be the same
(parameters passed in) for all strategies. The problem with the
second way is that there is no common base class, and that means that
I can't put these items on an STL queue (which requires that all its
types be homogeneous).
In my case, I want to make a general Task class, and put them on a
queue. Later, they will be taken off and executed by calling their
function do(). But in cases where you want to pass a few things in,
how do you do it? You could use Windows old trick of passing in 2
general parameters, one being a pointer to whatever you wish. But you
still need type information to interpret it.
Treat this as semi-pseudocode: You don't need this code to answer the
question.
// -------------------------
//First way: polymorphism
class base_strategy (string filename, int offset)
{
string filename, int offset;
base_strategy (string filename, int offset)
: filename(filename),offset(offset) {}
virtual void do() =0;
};
class strategy1 (string filename, int offset)
{
string filename, int offset;
strategy1 (string filename, int offset)
: filename(filename),offset(offset) {}
virtual void do() {//print contents of file starting at offset};
};
// But what if I want a strategy that also inputs another number?
// It's too late to change the interface.
// And isn't it illegal to have a container of pointers? Some STL
rule?
main()
{
queue<base_strategy *> q;
base_strategy *p = new strategy1 ("a.cpp", 234);
q.insert (p);
}
// ------------------------
//Second way: functors (function object)
class strategy1
{
int a,b;
strategy1 (int a, int b) : a(a), b(b) {};
void operator()(int i) {output a*b+2*i;}
}
for_each(q.begin(), q.end(), strategy1(3,4));
// That would work fine, but this doesn't help me with containers.
Sorry for the long message.