简单工厂模式

简介

简单工厂模式是类的创建模式,又名静态工厂方法,是由一个工厂类根据参数来决定创立出哪一种产品类的实例。

该模式中包含的角色及其职责:

工厂(Creator)角色

 简单工厂模式的核心,它负责实现创建所有实例的内部逻辑。工厂类可以被外界直接调用,创建所需的产品对象。

抽象产品(Product)角色

简单工厂模式所创建的所有对象的父类,它负责描述所有实例所共有的公共接口。

具体产品(Concrete Product)角色

是简单工厂模式的创建目标,所有创建的对象都是充当这个角色的某个具体类的实例。

优点

工厂类是整个模式的关键.包含了必要的逻辑判断,根据外界给定的信息,决定究竟应该创建哪个具体类的对象.通过使用工厂类,外界可以从直接创建具体产品 对象的尴尬局面摆脱出来,仅仅需要负责“消费”对象就可以了。而不必管这些对象究竟如何创建及如何组织的.明确了各自的职责和权利,有利于整个软件体系结 构的优化。

缺点

由于工厂类集中了所有实例的创建逻辑,违反了高内聚责任分配原则,将全部创建逻辑集中到了一个工厂类中;它所能创建的类只能是事先考虑到的,如果需要添加新的类,则就需要改变工厂类了。

当系统中的具体产品类不断增多时候,可能会出现要求工厂类根据不同条件创建不同实例的需求.这种对条件的判断和对具体产品类型的判断交错在一起,很难避免模块功能的蔓延,对系统的维护和扩展非常不利;

这些缺点在工厂方法模式中得到了一定的克服。

使用场景

工厂类负责创建的对象比较少;

客户只知道传入工厂类的参数,对于如何创建对象(逻辑)不关心;

由于简单工厂很容易违反高内聚责任分配原则,因此一般只在很简单的情况下应用。

 arithoperator.h中的代码如下:

  1 #ifndef ARITHOPERATOR_H
2 #define ARITHOPERATOR_H
3
4 #include <iostream>
5
6 using namespace std;
7
8 #define DIVIED_ZERO_ERROR 0
9 #define OPERATION_TYPE_ERROR 1
10
11 #define OK 0
12 #define ERROR 1
13
14 class arithOperator
15 {
16 protected:
17 double numberA;
18 double numberB;
19 public:
20 arithOperator()
21 {
22 numberA = 0;
23 numberB = 0;
24 }
25 void SetNumberA (double number)
26 {
27 numberA = number;
28 }
29 double GetNumberA()
30 {
31 return numberA;
32 }
33 void SetNumberB (double number)
34 {
35 numberB = number;
36 }
37 double GetNumberB ()
38 {
39 return numberB;
40 }
41
42 virtual double GetResult ()
43 {
44 double result = 0;
45 return result;
46 }
47 };
48
49 class OpeartionAdd : public arithOperator
50 {
51 virtual double GetResult ()
52 {
53 double result = 0;
54 result = numberA + numberB;
55 return result;
56 }
57 };
58
59 class OperationSub : public arithOperator
60 {
61 virtual double GetResult ()
62 {
63 double result = 0;
64 result = numberA - numberB;
65 return result;
66 }
67 };
68
69 class OperationMul : public arithOperator
70 {
71 virtual double GetResult ()
72 {
73 double result = 0;
74 result = numberA * numberB;
75 return result;
76 }
77 };
78
79 class OperationDiv : public arithOperator
80 {
81 virtual double GetResult ()
82 {
83 if (numberB == 0)
84 {
85 throw (DIVIED_ZERO_ERROR);
86 }
87
88 double result = 0;
89 result = numberA / numberB;
90
91 return result;
92 }
93 };
94
95
96 double ReadDouble ( char * tipMessage = "Input error!Please enter an double number again:\n")
97 {
98 double result = 0;
99
100 cin >> result;
101 while (cin.fail())
102 {
103 cout << tipMessage << endl;
104 cin.clear();
105 cin.sync();
106 cin >> result;
107 }
108 return result;
109 }
110
111 #endif // ARITHOPERATOR_H

factory.h中的代码如下:

 1 //simply Facotory
2 #ifndef FACTORY_H
3 #define FACTORY_H
4 #include "arithoperator.h"
5
6 class OperationFactory
7 {
8 public:
9 static arithOperator* createOperator( char operType)
10 {
11 arithOperator *operPointer = NULL;
12 switch (operType)
13 {
14 case '+':
15 operPointer = new OpeartionAdd();
16 break;
17
18 case '-':
19 operPointer = new OperationSub();
20 break;
21
22 case '*':
23 operPointer = new OperationMul();
24 break;
25
26 case '/':
27 operPointer = new OperationDiv();
28 break;
29
30 default:
31 throw OPERATION_TYPE_ERROR;
32 break;
33 }
34 return operPointer;
35 }
36 };
37
38
39 #endif // FACTORY_H

calculator.cpp中代码如下:

 1 #include <iostream>
2 #include "factory.h"
3 using namespace std;
4
5 int main (void)
6 {
7 try
8 {
9
10 cout << "Please enter an double number:" << endl;
11 double numberA;
12 numberA = ReadDouble();
13
14 cout << "Please enter an operator:" << endl;
15 char userOperator;
16 cin >> userOperator;
17
18 cout << "Please enter an double number:" << endl;
19 double numberB;
20 numberB = ReadDouble();
21
22 arithOperator *operPointer;
23 operPointer = OperationFactory::createOperator (userOperator);
24 operPointer->SetNumberA(numberA);
25 operPointer->SetNumberB(numberB);
26
27 double result = 0;
28 result = operPointer->GetResult();
29
30 cout << "The result is " << result << endl;
31
32 delete operPointer;
33
34 return OK;
35
36 }
37 catch ( int errorId )
38 {
39 switch (errorId)
40 {
41 case DIVIED_ZERO_ERROR:
42 cout << "Diving zero is an error!" << endl;
43 break;
44
45 case OPERATION_TYPE_ERROR:
46 cout << "Operator type is error!" << endl;
47 break;
48
49 default:
50 cout << "Unknown error!" << endl;
51 break;
52 }
53
54 return ERROR;
55 }
56
57 }

运行结果:


posted @ 2012-03-07 14:53  雨中枫叶  阅读(219)  评论(0编辑  收藏  举报