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

标签 c++ oop design-patterns factory-pattern

//抽象工厂类

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/

相关文章:

c# - ERROR_MORE_DATA ---- 从注册表读取

c++ - 使用 C++/Boost 绕过阻塞输入流

C++对象动态命名

asp.net-mvc - 为什么任何一种抽象都使用接口(interface)而不是抽象类?

c++ - 在列表中调用子对象

java - 根据类型标识符创建类

c++ - 如何从 C++ 中的函数返回矩阵(板)?

c++ - 如何在 C++ 的 boost::regex_replace() 中插入格式 str 并且不删除输入字符串中匹配的正则表达式?

java - struts - 每层的日志记录(Action、DAO)

design-patterns - 单态与单态