Question

I'm developing a class has decided to split it into independent strategies. Something like this:

template<class Strategy1, class Strategy2, class Strategy3>
class SomeClass : public ...
{
public:
    SomeClass()
    {
        obj1 = new Strategy1 // and etc initialize member class
    }

   void run()
    {
        // do something work with obj1, obj2, obj3
    }
    ...
private:
    Strategy1 Obj1;
    Strategy2 Obj2;
    Strategy3 Obj3;
};

The problem is that all the specific classes of strategies are trivial constructor, that is all initialized differently.

class Strategy1
{
public:
    Strategy1(int, int, int);
    ...
};

class Strategy2
{
public:
    Strategy2(int, double, std::vector&);
    ...
};

It turns out that if I'm only in the class initialization SomeClass I need to define a new SomeClass. What class and what should do initialization? Please tell me.

Was it helpful?

Solution

You should pass those Strategy classes into the constructor and build them outside of your containing object. If you attempt to do it wherein the construction is hidden within another class's constructor, you bind the implementation in a very hard manner.

 SomeClass(Strategy1 obj1, Strategy2 obj2, Strategy3 obj3){

Better yet, if you can use a TR1 function. Assuming no TR1, then I would suggest replacing those templates with calls to a Boost::function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top