Headfirst设计模式的C++实现——简单工厂模式(Simple Factory)之二

Posted Ren.Yu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Headfirst设计模式的C++实现——简单工厂模式(Simple Factory)之二相关的知识,希望对你有一定的参考价值。

为了引出后续的工厂方法,把在简单工厂模式的基础上增加了新功能——加盟店

简而言之就是把原来的单一简单工厂(能生产cheese和greek两种pizza)细分成了纽约地区的和芝加哥地区的(每种地区都能生产cheese和greek两种pizza)

和之前的简单工厂相比,PizzaStore中的PizzaFactory由构造时传入一个引用,从SimplePizzaFactory中派生出两个类,原来的cheese和greek Pizza也根据地区做了扩展。

Pizza.h

 1 #ifndef _PIZZA_H
 2 #define _PIZZA_H
 3 #include <iostream>
 4 #include <string>
 5 
 6 class Pizza
 7 {
 8 public:
 9     Pizza(const std::string &type) : m_type(type) {}
10     virtual ~Pizza() {}
11     virtual void prepare() { std::cout << "prepare " << m_type << std::endl; }
12     virtual void bake() { std::cout << "bake " << m_type << std::endl; }
13     virtual void cut() { std::cout << "cut " << m_type << std::endl; }
14     virtual void box() { std::cout << "box " << m_type << std::endl; }
15 private:
16     std::string m_type;
17 };
18 #endif

ChiChagoCheesePizza.h

 1 #ifndef _CHICHAGO_CHEESE_PIZZA_H
 2 #define _CHICHAGO_CHEESE_PIZZA_H
 3 #include <iostream>
 4 #include "Pizza.h"
 5 
 6 class ChiChagoCheesePizza : public Pizza
 7 {
 8 public:
 9     CChiChagoheesePizza() : Pizza("chichago cheese") {}
10     ~ChiChagoCheesePizza() {}
11 };
12 #endif

ChiChagoGreekPizza.h

 1 #ifndef _CHICHAGO_GREEK_PIZZA_H
 2 #define _CHICHAGO_GREEK_PIZZA_H
 3 #include <iostream>
 4 #include "Pizza.h"
 5 
 6 class ChiChagoGreekPizza : public Pizza
 7 {
 8 public:
 9     ChiChagoGreekPizza() : Pizza("chichago greek") {}
10     ~ChiChagoGreekPizza() {}
11 };
12 
13 #endif

NYCheesePizza.h

 1 #ifndef _NY_CHEESE_PIZZA_H
 2 #define _NY_CHEESE_PIZZA_H
 3 #include <iostream>
 4 #include "Pizza.h"
 5 
 6 class NYCheesePizza : public Pizza
 7 {
 8 public:
 9     NYCheesePizza() : Pizza("ny cheese") {}
10     ~NYCheesePizza() {}
11 };
12 #endif

 

NYGreekPizza.h

 1 #ifndef _NY_GREEK_PIZZA_H
 2 #define _NY_GREEK_PIZZA_H
 3 #include <iostream>
 4 #include "Pizza.h"
 5 
 6 class NYGreekPizza : public Pizza
 7 {
 8 public:
 9     NYGreekPizza() : Pizza("ny greek") {}
10     ~NYGreekPizza() {}
11 };
12 
13 #endif

SimplePizzaFactory.h

 1 #ifndef _SIMPLE_PIZZA_FACTORY
 2 #define _SIMPLE_PIZZA_FACTORY
 3 
 4 #include "Pizza.h"
 5 
 6 class SimplePizzaFactory
 7 {
 8 public:
 9     virtual Pizza *CreatePizza(const std::string &type) = 0;
10 };
11 #endif

 

ChiChagoPizzaFactory.h

 1 #ifndef _CHICHAGO_PIZZA_FACTORY_H
 2 #define _CHICHAGO_PIZZA_FACTORY_H
 3 
 4 #include "SimplePizzaFactory.h"
 5 #include "ChiChagoCheesePizza.h"
 6 #include "ChiChagoGreekPizza.h"
 7 
 8 class ChiChagoPizzaFactory : public SimplePizzaFactory
 9 {
10 public:
11     Pizza *CreatePizza(const std::string &type)
12     {
13         if ( "cheese" == type )
14         {
15             return new ChiChagoCheesePizza();
16         }
17         if ( "greek" == type )
18         {
19             return new ChiChagoGreekPizza();
20         }
21         return NULL;
22     }
23 };
24 #endif

 

NYPizzaFactory.h

 1 #ifndef _NY_PIZZA_FACTORY_H
 2 #define _NY_PIZZA_FACTORY_H
 3 
 4 #include "SimplePizzaFactory.h"
 5 #include "NYCheesePizza.h"
 6 #include "NYGreekPizza.h"
 7 
 8 class NYPizzaFactory : public SimplePizzaFactory
 9 {
10 public:
11     Pizza *CreatePizza(const std::string &type)
12     {
13         if ( "cheese" == type )
14         {
15             return new NYCheesePizza();
16         }
17         if ( "greek" == type )
18         {
19             return new NYGreekPizza();
20         }
21         return NULL;
22     }
23 };
24 #endif

PizzaStore.h

 1 #ifndef _PIZZA_STORE_H
 2 #define _PIZZA_STORE_H
 3 #include "SimplePizzaFactory.h"
 4 
 5 class PizzaStore
 6 {
 7 private:
 8     SimplePizzaFactory &m_pizza_factory;
 9 public:
10     PizzaStore(SimplePizzaFactory &pizza_factory) : m_pizza_factory(pizza_factory) {}
11     Pizza* OrderPizza(const std::string &type)
12     {
13         Pizza *p_pizza = m_pizza_factory.CreatePizza(type);
14         if (p_pizza)
15         {
16             p_pizza->prepare();
17             p_pizza->bake();
18             p_pizza->cut();
19             p_pizza->box();
20         }
21         return p_pizza;
22     }
23 };
24 #endif

 

main.cpp

 1 #include "PizzaStore.h"
 2 #include "NYPizzaFactory.h"
 3 int main()
 4 {
 5     NYPizzaFactory ny_pizza_factory;
 6     PizzaStore pizza_store(ny_pizza_factory);
 7     Pizza *p_pizza = pizza_store.OrderPizza("greek");
 8     if ( p_pizza )
 9     {
10         delete p_pizza;
11     }
12     return 0;
13 }

 

以上是关于Headfirst设计模式的C++实现——简单工厂模式(Simple Factory)之二的主要内容,如果未能解决你的问题,请参考以下文章

工厂模式C++实现

设计模式之工厂模式C++实现

设计模式之工厂模式C++实现

HeadFirst设计模式

工厂模式&抽象工厂——HeadFirst设计模式学习笔记

HeadFirst设计模式---简单工厂