//抽象工厂类
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类本身中是否有意义?
答案 0 :(得分:0)
将它们包含在pizzaStore类本身中是否有意义?
是,将这些方法放在class pizzaStore
static
方法中应该完全没问题。一般来说,我认为,根据class
类型将此工厂方法放在return
中,pizzaStore
是一个很好的选择。