抽象工厂模式 Abstract Factory Pattern C++

抽象工厂模式的C++实现版本。由于本人是初学C++,而且也是刚接触设计模式不久,所以非常希望有高手能指正程序中存在的问题,非常感谢。

抽象工厂类:

 1 AbstractFactory.h
 2 
 3 #ifndef _ABSTRACT_FACTORY_H
 4 #define _ABSTRACT_FACTORY_H 
 5 
 6 #include "Tax.h"
 7 #include "Bonus.h"
 8 
 9 //this is the abstract factory class
10 //there could be multiple abstract products in Abstract Factory Pattern
11 //but in Factory Method Pattern, there only could be one abstract product
12 
13 class AbstractFactory
14 {
15 public:    
16     virtual Tax* CreateTax() = 0;
17     virtual Bonus* CreateBonus() = 0;
18 };
19 
20 #endif

 

抽象产品类:

 1 Bonus.h
 2 
 3 #ifndef _BONUS_H
 4 #define _BONUS_H
 5 
 6 #include "Constant.h"
 7 
 8 //this is an abstract product class
 9 
10 class Bonus
11 {
12 public:
13     virtual double Calculate() = 0;
14 };
15 
16 #endif
17 
18 Tax.h
19 
20 #ifndef _TAX_H
21 #define _TAX_H
22 
23 #include "Constant.h"
24 
25 //this is an abstract product class
26 
27 class Tax
28 {
29 public:
30     virtual double Calculate() = 0;
31 };
32 
33 #endif

具体工厂类:

 1 AmericanFactory.h
 2 
 3 #ifndef _AMERICAN_FACTORY_H
 4 #define _AMERICAN_FACTORY_H
 5 
 6 #include "AbstractFactory.h"
 7 #include "AmericanTax.h"
 8 #include "AmericanBonus.h"
 9 
10 //this is a concrete factory class
11 //the concrete factory in Abstract Factory Pattern can create multiple concrete products
12 //but in Factory Method Pattern, the concrete factory only can create one concrete product
13 
14 class AmericanFactory : public AbstractFactory
15 {
16 public:
17     virtual Tax* CreateTax();
18     virtual Bonus* CreateBonus();
19 };
20 
21 
22 #endif
23 
24 AmericanFactory.cpp
25 
26 #include "AmericanFactory.h"
27 
28 Tax* AmericanFactory::CreateTax()
29 {
30     Tax* p = new AmericanTax();
31     return p;
32 }
33 
34 Bonus* AmericanFactory::CreateBonus()
35 {
36     Bonus* p= new AmericanBonus();
37     return p;
38 }
39 
40 ChineseFactory.h
41 
42 #ifndef _CHINESE_FACTORY_H
43 #define _CHINESE_FACTORY_H
44 
45 #include "AbstractFactory.h"
46 #include "ChineseTax.h"
47 #include "ChineseBonus.h"
48 
49 //this is a concrete factory class
50 
51 class ChineseFactory : public AbstractFactory
52 {
53 public:
54     virtual Tax* CreateTax();
55     virtual Bonus* CreateBonus();
56 };
57 
58 
59 #endif
60 
61 ChineseFactory.cpp
62 
63 #include "ChineseFactory.h"
64 
65 Tax* ChineseFactory::CreateTax()
66 {
67     Tax* p = new ChineseTax();
68     return p;
69 }
70 
71 Bonus* ChineseFactory::CreateBonus()
72 {
73     Bonus* p = new ChineseBonus();
74     return p;
75 }

具体产品类:

  1 AmericanTax.h
  2 
  3 #ifndef _AMERICAN_TAX_H
  4 #define _AMERICAN_TAX_H
  5 
  6 #include "Tax.h"
  7 
  8 //this is a concrete product class
  9 
 10 class AmericanTax : public Tax
 11 {
 12 public:
 13     virtual double Calculate();
 14 };
 15 
 16 #endif
 17 
 18 AmericanTax.cpp
 19 
 20 #include "AmericanTax.h"
 21 
 22 double AmericanTax::Calculate()
 23 {
 24     return Constant.BASE_SALARY * 0.1;
 25 }
 26 
 27 AmericanBonus.h
 28 
 29 #ifndef _AMERICAN_BONUS_H
 30 #define _AMERICAN_BONUS_H
 31 
 32 #include "Bonus.h"
 33 
 34 //this is a concrete product class
 35 
 36 class AmericanBonus : public Bonus
 37 {
 38 public:
 39     virtual double Calculate();
 40 };
 41 
 42 #endif
 43 
 44 AmericanBonus.cpp
 45 
 46 #include "AmericanBonus.h"
 47 
 48 double AmericanBonus::Calculate ()
 49 {
 50     return Constant.BASE_SALARY * 0.4;
 51 }
 52 
 53 ChineseTax.h
 54 
 55 #ifndef _CHINESE_TAX_H
 56 #define _CHINESE_TAX_H
 57 
 58 #include "Tax.h"
 59 
 60 //this is a concrete product class
 61 
 62 class ChineseTax : public Tax
 63 {
 64 public:
 65     virtual double Calculate();
 66 };
 67 
 68 #endif
 69 
 70 ChineseTax.cpp
 71 
 72 #include "ChineseTax.h"
 73 
 74 double ChineseTax::Calculate()
 75 {
 76     return Constant.BASE_SALARY *0.4;
 77 }
 78 
 79 ChineseBonus.h
 80 
 81 #ifndef _CHINESE_BONUS_H
 82 #define _CHINESE_BONUS_H
 83 
 84 #include "Bonus.h"
 85 
 86 //this is a concrete product class
 87 
 88 class ChineseBonus : public Bonus
 89 {
 90 public:
 91     virtual double Calculate();
 92 };
 93 
 94 #endif
 95 
 96 ChineseBonus.cpp
 97 
 98 #include "ChineseBonus.h"
 99 
100 double ChineseBonus::Calculate ()
101 {
102     return Constant.BASE_SALARY * 0.1;
103 }

常量:

 1 Constant.h
 2 
 3 #ifndef _CONSTANT_H
 4 #define _CONSTANT_H
 5 
 6 class Constant
 7 {
 8 public:
 9     static const double BASE_SALARY;
10 };
11 
12 #endif
13 
14 Constant.cpp
15 
16 #include "Constant.h"
17 
18 const double Constant::BASE_SALARY = 4000;

调用代码:

 1 Client.cpp
 2 
 3 
 4 #include <iostream>
 5 #include "AbstractFactory.h"
 6 #include "AmericanFactory.h"
 7 #include "AmericanBonus.h"
 8 #include "AmericanTax.h"
 9 #include "ChineseFactory.h"
10 #include "ChineseBonus.h"
11 #include "ChineseTax.h"
12 
13 //use this macro to determine which policy to use 
14 #define USE_AMERICAN_POLICY
15 
16 AbstractFactory* GetConcreteFactory()
17 {
18     AbstractFactory* factory;
19 
20 #ifndef USE_AMERICAN_POLICY
21     factory = new ChineseFactory();
22 #else
23     factory = new AmericanFactory();
24 #endif
25 
26     return factory;
27 }
28 
29 using namespace std;
30 
31 int main(int argc, char** argv)
32 {
33 
34     AbstractFactory* factory = GetConcreteFactory();
35 
36     Tax* tax = factory->CreateTax(); 
37     Bonus* bonus = factory->CreateBonus();
38 
39     double salary = Constant.BASE_SALARY - tax->Calculate()  + bonus->Calculate();
40 
41     cout<<"Salary is: "<< salary <<endl;
42 
43     delete factory;
44     delete tax;
45     delete bonus;
46 
47     return 0;
48 }


工厂方法模式与抽象工厂模式的区别:
工厂方法模式只有一个抽象产品类,而抽象工厂模式有多个。
工厂方法模式的具体工厂类只能创建一个具体产品类的实例,而抽象工厂模式可以创建多个。

 

开发环境:VC 6.0

 

参考:http://terrylee.cnblogs.com/archive/2005/12/13/295965.html

posted on 2013-05-07 23:36  亲爱的西西哥  阅读(205)  评论(1)    收藏  举报

导航