005 --- 第8章 工厂方法模式

简述:

  工厂方法模式定义一个用于创建对象的接口,让子类决定实例化哪个类,工厂方法使一个类的实例化延迟到其子类。

  工厂方法模式包括:抽象产品类、具体产品类、抽象工厂方法类、具体工厂方法类。

    抽象产品类:定义工厂方法所创建的对象的接口。

    具体产品类:继承自抽象产品类,实现了抽象产品类的接口。

    抽象工厂方法类:返回一个抽象产品类的对象。

    具体工厂方法类:重定义抽象工厂方法以返回具体产品的对象。

 

工厂方法模式代码:

  1 #include <iostream>
  2 using namespace std;
  3 
  4 //工厂方法模式
  5 // 抽象产品类
  6 class COperation
  7 {
  8 private:
  9     double m_dA = 0;
 10     double m_dB = 0;
 11 public:
 12     void SetA(double dA)
 13     {
 14         m_dA = dA;
 15     }
 16     double GetA()
 17     {
 18         return m_dA;
 19     }
 20     void SetB(double dB)
 21     {
 22         m_dB = dB;
 23     }
 24     double GetB()
 25     {
 26         return m_dB;
 27     }
 28     virtual double GetResult()
 29     {
 30         return 0;
 31     };
 32 };
 33 
 34 // 加法类(具体产品类)
 35 class COperationAdd : public COperation
 36 {
 37 public:
 38     virtual double GetResult()
 39     {
 40         double dResult = 0;
 41         dResult = GetA() + GetB();
 42         return dResult;
 43     }
 44 };
 45 
 46 // 减法类(具体产品类)
 47 class COperationSub : public COperation
 48 {
 49 public:
 50     virtual double GetResult()
 51     {
 52         double dResult = 0;
 53         dResult = GetA() - GetB();
 54         return dResult;
 55     }
 56 };
 57 
 58 // 乘法类(具体产品类)
 59 class COperationMul : public COperation
 60 {
 61 public:
 62     virtual double GetResult()
 63     {
 64         double dResult = 0;
 65         dResult = GetA() * GetB();
 66         return dResult;
 67     }
 68 };
 69 
 70 // 除法类(具体产品类)
 71 class COperationDiv : public COperation
 72 {
 73 public:
 74     virtual double GetResult()
 75     {
 76         double dResult = 0;
 77         if (0 == GetB())
 78             throw "除数不能为0!";
 79         dResult = GetA() / GetB();
 80         return dResult;
 81     }
 82 };
 83 
 84 // 抽象工厂类
 85 class CFactory
 86 {
 87 public:
 88     virtual COperation* CreateOperation()
 89     {
 90         return NULL;
 91     }
 92 };
 93 
 94 // 加法工厂(具体工厂类)
 95 class CAddFactory : public CFactory
 96 {
 97 public:
 98     virtual COperation* CreateOperation()
 99     {
100         return new COperationAdd();
101     }
102 };
103 
104 // 减法工厂(具体工厂类)
105 class CSubFactory : public CFactory
106 {
107 public:
108     virtual COperation* CreateOperation()
109     {
110         return new COperationSub();
111     }
112 };
113 
114 // 乘法工厂(具体工厂类)
115 class CMulFactory : public CFactory
116 {
117 public:
118     virtual COperation* CreateOperation()
119     {
120         return new COperationMul();
121     }
122 };
123 
124 // 除法工厂(具体工厂类)
125 class CDivFactory : public CFactory
126 {
127 public:
128     virtual COperation* CreateOperation()
129     {
130         return new COperationDiv();
131     }
132 };
133 
134 int main()
135 {
136     CFactory* pFactory = new CAddFactory();
137     COperation* pOperation = pFactory->CreateOperation();
138     if (pOperation)
139     {
140         pOperation->SetA(1);
141         pOperation->SetB(2);
142         double dResult = pOperation->GetResult();
143         cout << dResult << endl;
144 
145         delete pOperation;
146         pOperation = NULL;
147     }
148 
149     system("pause");
150     return 0;
151 }

运行结果:

例:活雷锋

代码如下:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 // 雷锋(抽象产品类)
 5 class CLeiFeng
 6 {
 7 public:
 8     void Sweep()
 9     {
10         cout << "扫地" << endl;
11     }
12 
13     void Wash()
14     {
15         cout << "洗衣" << endl;
16     }
17 
18     void ByrICE()
19     {
20         cout << "买米" << endl;
21     }
22 };
23 
24 // 学雷锋的大学生(具体产品类)
25 class CUndergraduate : public CLeiFeng
26 {
27 public:
28     CUndergraduate()
29     {
30         cout << "学雷锋的大学生" << endl;
31     }
32 };
33 
34 // 社区志愿者(具体产品类)
35 class CVolunteer : public CLeiFeng
36 {
37 public:
38     CVolunteer()
39     {
40         cout << "社区志愿者" << endl;
41     }
42 };
43 
44 // 雷锋工厂(抽象工厂类)
45 class CFactory
46 {
47 public:
48     virtual CLeiFeng* CreateLeiFeng()
49     {
50         return NULL;
51     }
52 };
53 
54 // 学雷锋的大学生工厂(具体工厂类)
55 class CUndergraduateFactory : public CFactory
56 {
57 public:
58     virtual CLeiFeng* CreateLeiFeng()
59     {
60         return new CUndergraduate();
61     }
62 };
63 
64 // 社区志愿者工厂(具体工厂类)
65 class CVolunteerFactory : public CFactory
66 {
67 public:
68     virtual CLeiFeng* CreateLeiFeng()
69     {
70         return new CVolunteer();
71     }
72 };
73 
74 int main()
75 {
76     CFactory* pFactory = new CUndergraduateFactory();
77     CLeiFeng* pStudent = pFactory->CreateLeiFeng();
78 
79     if (pStudent)
80     {
81         pStudent->Sweep();
82         pStudent->Wash();
83         pStudent->ByrICE();
84 
85         delete pStudent;
86         pStudent = NULL;
87     }
88     if (pFactory)
89     {
90         delete pFactory;
91         pFactory = NULL;
92     }
93 
94     system("pause");
95     return 0;
96 }

运行结果:

原文地址:https://www.cnblogs.com/SmallAndGreat/p/13474073.html