为了引出后续的工厂方法,把在简单工厂模式的基础上增加了新功能——加盟店
简而言之就是把原来的单一简单工厂(能生产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 ;
}