Chinaunix首页 | 论坛 | 博客
  • 博客访问: 48599
  • 博文数量: 14
  • 博客积分: 297
  • 博客等级: 二等列兵
  • 技术积分: 547
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-08 21:05
个人简介

努力做自己不喜欢的事情!

文章分类
文章存档

2013年(4)

2012年(10)

我的朋友

分类: C/C++

2012-07-30 20:11:20

抽象工厂模式
 
定义:
    抽象工厂模式提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。
 
ClassDiagram:
 
图中,是强调CheeseStore的原料Ingredient选择的抽象工厂,四种原料来自南北两方。
 
 
PizzaIngredientFactory.h
  1. #if !defined(_PIZZAINGREDIENTFACTORY_H)
  2. #define _PIZZAINGREDIENTFACTORY_H
  3. #include "Dough.h"
  4. #include "Sauce.h"
  5. #include "Cheese.h"
  6. #include "Clam.h"

  7. class PizzaIngredientFactory
  8. {
  9. public:
  10.     virtual Dough* createDough() = 0;
  11.     virtual Sauce* createSauce() = 0;
  12.     virtual Cheese* createCheese() = 0;
  13.     virtual Clam* createClam() = 0;
  14. };

  15. #endif //_PIZZAINGREDIENTFACTORY_H
NorthCheesePizzaIngredientFactory.h
  1. #if !defined(_NORTHCHEESEPIZZAINGREDIENTFACTORY_H)
  2. #define _NORTHCHEESEPIZZAINGREDIENTFACTORY_H

  3. #include "PizzaIngredientFactory.h"
  4. #include "Dough.h"
  5. #include "Sauce.h"
  6. #include "Cheese.h"
  7. #include "Clam.h"
  8. class NorthCheesePizzaIngredientFactory : public PizzaIngredientFactory
  9. {
  10. public:
  11.     Dough* createDough();
  12.     Sauce* createSauce();
  13.     Cheese* createCheese();
  14.     Clam* createClam();
  15. };

  16. #endif //_NORTHCHEESEPIZZAINGREDIENTFACTORY_H

Cheese.h
  1. #if !defined(_CHEESE_H)
  2. #define _CHEESE_H


  3. class Cheese
  4. {
  5. };

  6. #endif //_CHEESE_H
NorthCheese.h
  1. #if !defined(_NORTHCHEESE_H)
  2. #define _NORTHCHEESE_H

  3. #include "Cheese.h"

  4. class NorthCheese : public Cheese
  5. {
  6. public:
  7.     NorthCheese();
  8. };

  9. #endif //_NORTHCHEESE_H
CheesePizzaStore.h
  1. #if !defined(_CHEESEPIZZASTORE_H)
  2. #define _CHEESEPIZZASTORE_H
  3. #include <string>

  4. using namespace std;

  5. class CheesePizzaStore
  6. {
  7. public:
  8.     void createPizza(string type);
  9. };

  10. #endif //_CHEESEPIZZASTORE_H

CheesePizzaStore.cpp
  1. #include "stdafx.h"
  2. #include "CheesePizzaStore.h"

  3. #include "NorthCheesePizzaIngredientFactory.h"
  4. #include "SouthCheesePizzaIngredientFactory.h"
  5. #include "PizzaIngredientFactory.h"
  6. void CheesePizzaStore::createPizza(string type)
  7. {
  8.     PizzaIngredientFactory* pf;
  9.     if (type == "North")
  10.     {
  11.         pf = new NorthCheesePizzaIngredientFactory();
  12.     }
  13.     else if(type == "South")
  14.     {
  15.         pf = new SouthCheesePizzaIngredientFactory();
  16.     }
  17.     
  18.     pf->createCheese();
  19.     pf->createClam();
  20.     pf->createDough();
  21.     pf->createSauce();
  22.     
  23. }
Test.cpp
  1. #include "stdafx.h"
  2. #include "CheesePizzaStore.h"
  3. #include "NorthCheesePizzaIngredientFactory.h"
  4. #include "SouthCheesePizzaIngredientFactory.h"
  5. #include "PizzaIngredientFactory.h"
  6. int main(int argc, char* argv[])
  7. {
  8.     printf("Hello World!\n");
  9.     
  10.     CheesePizzaStore* cps = new CheesePizzaStore();
  11.     cps->createPizza("North");
  12.     printf("\n----------------------------\n");
  13.     cps->createPizza("South");
  14.     printf("\n");
  15.     return 0;
  16. }

Output:
  1. Hello
  2. NorthCheese.NorthClam.NorthDough.NorthSauce.
  3. ----------------------------
  4. SouthCheese.SouthClam.SouthDough.SouthSauce.
  5. Press any key to continue

完整的项目源码,VC6.0工程。
阅读(852) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~