Headfirst设计模式的C++实现——抽象工厂(Abstract Factory)

Posted Ren.Yu

tags:

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

Dough.h

1 #ifndef _DOUGH_H
2 #define _DOUGH_H
3 
4 class Dough
5 {
6 };
7 #endif

 

ThinCrustDough.h

1 #ifndef _THIN_CRUST_DOUGH_H
2 #define _THIN_CRUST_DOUGH_H
3 
4 #include "Dough.h"
5 
6 class ThinCrustDough : public Dough
7 {
8 };
9 #endif

 

Sauce.h

1 #ifndef _SAUCE_H
2 #define _SAUCE_H
3 
4 class Sauce
5 {
6 };
7 #endif

 

MarinaraSauce.h

1 #ifndef _MARINARA_SAUCE_H
2 #define _MARINARA_SAUCE_H
3 
4 #include "Sauce.h"
5 
6 class MarinaraSauce : public Sauce
7 {
8 };
9 #endif

 

Pizza.h

 1 #ifndef _PIZZA_H
 2 #define _PIZZA_H
 3 #include <iostream>
 4 #include <string>
 5 #include "Dough.h"
 6 #include "Sauce.h"
 7 class Pizza
 8 {
 9 public:
10     Pizza() : m_name(), m_p_dough(NULL), m_p_sauce(NULL) {}
11     virtual ~Pizza() {}
12     virtual void prepare() = 0; 
13     virtual void bake() { std::cout << "Bake for 25 mins at 350" << std::endl; }
14     virtual void cut() { std::cout << "Cutting the pizza into diagonal slices" << std::endl; }
15     virtual void box() { std::cout << "Place pizza in official PizzaStore box" << std::endl; }
16     void set_name(const std::string &name) { m_name = name; }
17     std::string get_name() { return m_name; }
18     Dough *m_p_dough;
19     Sauce *m_p_sauce;
20 private:
21     std::string m_name;
22 };
23 #endif

 

CheesePizza.h

 1 #ifndef _CHEESE_PIZZA_H
 2 #define _CHEESE_PIZZA_H
 3 
 4 #include "Pizza.h"
 5 #include "PizzaIngredientFactory.h"
 6 
 7 class CheesePizza : public Pizza
 8 {
 9 private:
10     PizzaIngredientFactory *m_p_ingredient_factory;
11 public:
12     CheesePizza(PizzaIngredientFactory *p) : m_p_ingredient_factory(p) {}
13     void prepare()
14     {
15         std::cout << "Preparing " << get_name() << std::endl;
16         m_p_sauce = m_p_ingredient_factory->create_sauce();
17     }
18 };
19 
20 #endif

 

GreekPizza.h

 1 #ifndef _GREEK_PIZZA_H
 2 #define _GREEK_PIZZA_H
 3 
 4 #include "Pizza.h"
 5 #include "PizzaIngredientFactory.h"
 6 
 7 class GreekPizza : public Pizza
 8 {
 9 private:
10     PizzaIngredientFactory *m_p_ingredient_factory;
11 public:
12     GreekPizza(PizzaIngredientFactory *p) : m_p_ingredient_factory(p) {}
13     void prepare()
14     {
15         std::cout << "Preparing " << get_name() << std::endl;
16         m_p_dough = m_p_ingredient_factory->create_dough();
17     }
18 };
19 
20 #endif

 

PizzaStore.h

 1 #ifndef _PIZZA_STORE_H
 2 #define _PIZZA_STORE_H
 3 
 4 #include "Pizza.h"
 5 
 6 class PizzaStore
 7 {
 8 private:
 9     virtual Pizza* CreatePizza(const std::string &type) = 0;
10 public:
11     Pizza* OrderPizza(const std::string &type)
12     {
13         Pizza *p_pizza = 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

 

NYPizzaStore.h

 1 #ifndef _NY_PIZZA_STORE_H
 2 #define _NY_PIZZA_STORE_H
 3 
 4 #include "PizzaStore.h"
 5 #include "CheesePizza.h"
 6 #include "GreekPizza.h"
 7 #include "NYPizzaIngredientFactory.h"
 8 class NYPizzaStore : public PizzaStore
 9 {
10 private:
11     Pizza* CreatePizza(const std::string &type)
12     {
13         PizzaIngredientFactory *p_factory = new NYPizzaIngredientFactory();
14         if ( "cheese" == type )
15         {
16             Pizza *p_pizza = new CheesePizza( p_factory );
17             p_pizza->set_name("New York Style Cheese Pizza");
18             return p_pizza; 
19         }
20         if ( "greek" == type )
21         {
22             Pizza *p_pizza = new GreekPizza( p_factory );
23             p_pizza->set_name("New York Style Greek Pizza");
24             return p_pizza; 
25         }
26         return NULL;
27     } 
28 };
29 #endif

 

PizzaIngredientFactory.h

 1 #ifndef _PIZZA_INGREDIENT_FACTORY_H
 2 #define _PIZZA_INGREDIENT_FACTORY_H
 3 
 4 #include "Dough.h"
 5 #include "Sauce.h"
 6 
 7 class PizzaIngredientFactory
 8 {
 9 public:
10     virtual Dough* create_dough() = 0;
11     virtual Sauce* create_sauce() = 0;
12 };
13 
14 #endif

 

NYPizzaIngredientFactory.h

 1 #ifndef _NY_PIZZA_INGREDIENT_FACTORY_H
 2 #define _NY_PIZZA_INGREDIENT_FACTORY_H
 3 
 4 #include "ThinCrustDough.h"
 5 #include "MarinaraSauce.h"
 6 
 7 class NYPizzaIngredientFactory : public PizzaIngredientFactory
 8 {
 9 public:
10     Dough* create_dough() { std::cout << "Creating Thin Crust Dough" << std::endl; return new ThinCrustDough(); }
11     Sauce* create_sauce() { std::cout << "Creating Marinara Sauce" << std::endl; return new MarinaraSauce(); }
12 };
13 
14 #endif

 

main.cpp

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

 

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

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

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

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

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

HeadFirst设计模式

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