gpt4 book ai didi

c++ - 抽象工厂模式中将工厂方法插入哪里?

转载 作者:行者123 更新时间:2023-12-02 10:26:16 25 4
gpt4 key购买 nike

//抽象工厂类

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com