為了引出後續的工廠方法,把在簡單工廠模式的基礎上增加了新功能——加盟店

簡而言之就是把原來的單一簡單工廠(能生產cheese和greek兩種pizza)細分成了紐約地區的和芝加哥地區的(每種地區都能生產cheese和greek兩種pizza)

和之前的簡單工廠相比,PizzaStore中的PizzaFactory由構造時傳入一個引用,從SimplePizzaFactory中派生出兩個類,原來的cheese和greek Pizza也根據地區做了擴充套件。

Pizza.h

 #ifndef _PIZZA_H
#define _PIZZA_H
#include <iostream>
#include <string> class Pizza
{
public:
Pizza(const std::string &type) : m_type(type) {}
virtual ~Pizza() {}
virtual void prepare() { std::cout << "prepare " << m_type << std::endl; }
virtual void bake() { std::cout << "bake " << m_type << std::endl; }
virtual void cut() { std::cout << "cut " << m_type << std::endl; }
virtual void box() { std::cout << "box " << m_type << std::endl; }
private:
std::string m_type;
};
#endif

ChiChagoCheesePizza.h

 #ifndef _CHICHAGO_CHEESE_PIZZA_H
#define _CHICHAGO_CHEESE_PIZZA_H
#include <iostream>
#include "Pizza.h" class ChiChagoCheesePizza : public Pizza
{
public:
CChiChagoheesePizza() : Pizza("chichago cheese") {}
~ChiChagoCheesePizza() {}
};
#endif

ChiChagoGreekPizza.h

 #ifndef _CHICHAGO_GREEK_PIZZA_H
#define _CHICHAGO_GREEK_PIZZA_H
#include <iostream>
#include "Pizza.h" class ChiChagoGreekPizza : public Pizza
{
public:
ChiChagoGreekPizza() : Pizza("chichago greek") {}
~ChiChagoGreekPizza() {}
}; #endif

NYCheesePizza.h

 #ifndef _NY_CHEESE_PIZZA_H
#define _NY_CHEESE_PIZZA_H
#include <iostream>
#include "Pizza.h" class NYCheesePizza : public Pizza
{
public:
NYCheesePizza() : Pizza("ny cheese") {}
~NYCheesePizza() {}
};
#endif

NYGreekPizza.h

 #ifndef _NY_GREEK_PIZZA_H
#define _NY_GREEK_PIZZA_H
#include <iostream>
#include "Pizza.h" class NYGreekPizza : public Pizza
{
public:
NYGreekPizza() : Pizza("ny greek") {}
~NYGreekPizza() {}
}; #endif

SimplePizzaFactory.h

 #ifndef _SIMPLE_PIZZA_FACTORY
#define _SIMPLE_PIZZA_FACTORY #include "Pizza.h" class SimplePizzaFactory
{
public:
virtual Pizza *CreatePizza(const std::string &type) = ;
};
#endif

ChiChagoPizzaFactory.h

 #ifndef _CHICHAGO_PIZZA_FACTORY_H
#define _CHICHAGO_PIZZA_FACTORY_H #include "SimplePizzaFactory.h"
#include "ChiChagoCheesePizza.h"
#include "ChiChagoGreekPizza.h" class ChiChagoPizzaFactory : public SimplePizzaFactory
{
public:
Pizza *CreatePizza(const std::string &type)
{
if ( "cheese" == type )
{
return new ChiChagoCheesePizza();
}
if ( "greek" == type )
{
return new ChiChagoGreekPizza();
}
return NULL;
}
};
#endif

NYPizzaFactory.h

 #ifndef _NY_PIZZA_FACTORY_H
#define _NY_PIZZA_FACTORY_H #include "SimplePizzaFactory.h"
#include "NYCheesePizza.h"
#include "NYGreekPizza.h" class NYPizzaFactory : public SimplePizzaFactory
{
public:
Pizza *CreatePizza(const std::string &type)
{
if ( "cheese" == type )
{
return new NYCheesePizza();
}
if ( "greek" == type )
{
return new NYGreekPizza();
}
return NULL;
}
};
#endif

PizzaStore.h

 #ifndef _PIZZA_STORE_H
#define _PIZZA_STORE_H
#include "SimplePizzaFactory.h" class PizzaStore
{
private:
SimplePizzaFactory &m_pizza_factory;
public:
PizzaStore(SimplePizzaFactory &pizza_factory) : m_pizza_factory(pizza_factory) {}
Pizza* OrderPizza(const std::string &type)
{
Pizza *p_pizza = m_pizza_factory.CreatePizza(type);
if (p_pizza)
{
p_pizza->prepare();
p_pizza->bake();
p_pizza->cut();
p_pizza->box();
}
return p_pizza;
}
};
#endif

main.cpp

 #include "PizzaStore.h"
#include "NYPizzaFactory.h"
int main()
{
NYPizzaFactory ny_pizza_factory;
PizzaStore pizza_store(ny_pizza_factory);
Pizza *p_pizza = pizza_store.OrderPizza("greek");
if ( p_pizza )
{
delete p_pizza;
}
return ;
}