I'm trying to implement a Strategy design pattern in C++.
I have an abstract class with no non-abstract method called ICookingStrategy:
class ICookingStrategy
{
public:
virtual int cook() = 0;
};
From this abstract class inherit two child classes: fastCooking and slowCooking:
class fastCooking : public ICookingStrategy
{
public:
int cook() { return 0; }
};
class slowCooking : public ICookingStrategy
{
public:
int cook() { return 0; }
};
The point is, I want another class to be populated by an object instanciating either fastCooking or slowCooking, indifferently. Which means, an object instantiating ICookingStrategy.
But my problem is: I can't find how I can populate this receiving class. I thought in the constructor's arguments, but when I try this, g++ gives me an error:
class CookingContext
{
public:
CookingContext (ICookingStrategy* cookingStrategy):cookingStrategy(cookingStrategy)
{}
int cook()
{
this->cookingStrategy.cook();
return 0;
}
private:
ICookingStrategy* cookingStrategy;
};
In main()
, I want to do instantiate one of the two child classes, then populate CookingContext
like this:
int main(void)
{
ICookingStrategy* cookingStrategy;
cookingStrategy = new fastCooking;
CookingContext cooking(cookingStrategy);
cooking.cook();
return 0;
}
G++ error message on compilation is:
error: expected ‘)’ before ‘*’ token
CookingContext(ICookingStrategy* cookingMode):cookingMode(cookingMode)
^
What is the best way to make CookingContext
being populated by the strategy
object, instantiating either child of ICookingStrategy
?
(tried many different ones, and didn't manage to make it work)
Your constructor should take ownership of a caller-provided instance of one of the concrete cooking strategies, but needs to do so by pointer:
CookingContext(ICookingStrategy* cookingStrategy): cookingStrategy(cookingStrategy) {...}
ICookingStrategy* cookingStrategy;
int cook()
{
return cookingStrategy->cook();
}
A destructor should be added to delete the cooking strategy:
~CookingContext() { delete cookingStrategy; }