//抽象工厂类
class pizzaStore
{
public:
vector <string> toppingsType;
virtual void bakePizza (){}
virtual void preparePizza ()
{
for (int i = 0; i < toppingsType.size (); i++)
{
addTopping (toppingsType[i]);
}
}
virtual void cutPizza (){}
virtual void boxPizza (){}
};
class xPizzaStore : public pizzaStore
{
xPizzaStore ()
{
toppingsType = "1";
sauceType = "2";
cheesetype = "3";
}
void orderPizza ()
{
bakePizza ();
preparePizza ();
// xPizzaStore doesn't cut the pizza.
boxPizza ();
}
};
//工厂创建工厂的方法
pizzaStore * whichStore (string storeName)
{
pizzaStore obj = NULL;
if (storeName == "x")
{
obj = new xPizzaStore ();
}
else if (storeName == "y")
{
obj = new yPizzaStore ();
}
else if (storeName == "z")
{
obj = new zPizzaStore ();
}
return obj;
}
//制作披萨的工厂方法
pizzaStore * whichPizza (string pizzaName)
{
pizzaName obj = NULL;
if (pizzaBaseType == "x")
{
obj = new xPizzaName ();
}
else if (pizzaBaseType == "y")
{
obj = new yPizzaName ();
}
else if (pizzaBaseType == "z")
{
obj = new zPizzaName ();
}
return obj;
}
可能有不同类型的商店以及比萨饼。
这两个工厂方法应包含在哪个类中,为什么?
将它们包含在pizzaStore类本身中是否有意义?
最佳答案
Does it make sense to include them in the pizzaStore class itself?
是,将那些方法作为
class pizzaStore
方法放在static
中应该很好。通常,我考虑将这种工厂方法根据其class
类型放在return
中,并且pizzaStore
是一个不错的选择。
关于c++ - 抽象工厂模式中将工厂方法插入哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9044875/