简单工厂模式

原文:http://blog.youkuaiyun.com/tianshuai11/article/details/7671097#comments

一,概念

从设计模式的类型上来说,简单工厂模式是属于创建型模式,又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。


二,菜鸟实现

  1. #include<iostream>
  2. #include<exception>
  3. usingnamespacestd;
  4. intmain()
  5. {
  6. try{
  7. stringa,b;
  8. charop;
  9. intresult;
  10. cout<<"请输入数字A"<<endl;
  11. cin>>a;
  12. cout<<"请输入运算符(+,-,*,/)"<<endl;
  13. cin>>op;
  14. cout<<"请输入数字B"<<endl;
  15. cin>>b;
  16. intA=atoi(a.c_str());
  17. intB=atoi(b.c_str());
  18. switch(op)
  19. {
  20. case'+':result=A+B;break;
  21. case'-':result=A-B;break;
  22. case'*':result=A*B;break;
  23. case'/':
  24. if(B==0)
  25. result=-9999;
  26. else
  27. result=A/B;
  28. break;
  29. default:result=-9999;
  30. }
  31. cout<<"result="<<result<<endl;
  32. }
  33. catch(exception&e)
  34. {
  35. cout<<"Exception:"<<e.what()<<endl;
  36. }
  37. return0;
  38. }
分析:程序实现还算可以,注意到了: 1)除数为0的判断,和异常的检查

2)switch的应用,在C/C++中switch之接受 char 和 int 类型。其他的类型只能用if


存在的问题: 1)首先没有按照面向对象的设计方式实现。

2)不容易维护

3)不容易复用:控制台改成图形界面?只能复制代码,然后更改代码。不能直接调用代码类

4)不容易扩展:如果要添加 sqrt( ) 开方?如何实现?

5)灵活性不好:操作类和界面类没有分开。

解决办法:通过面向对象的三大技术封装、继承和多态


首先应用封装,让业务逻辑、界面逻辑分开,降低耦合度。

菜鸟进阶一:

  1. #include<iostream>
  2. #include<exception>
  3. usingnamespacestd;
  4. classOperation
  5. {
  6. public:
  7. staticintGetResult(intA,intB,charop)//static静态类,所有对象共享一个方法
  8. {
  9. intresult;
  10. switch(op)
  11. {
  12. case'+':result=A+B;break;
  13. case'-':result=A-B;break;
  14. case'*':result=A*B;break;
  15. case'/':
  16. if(B==0)
  17. result=-9999;
  18. else
  19. result=A/B;
  20. break;
  21. default:result=-9999;
  22. }
  23. returnresult;
  24. }
  25. };
  26. intmain()
  27. {
  28. Operationoperation;
  29. try{
  30. stringa,b;
  31. charop;
  32. intresult;
  33. cout<<"请输入数字A"<<endl;
  34. cin>>a;
  35. cout<<"请输入运算符(+,-,*,/)"<<endl;
  36. cin>>op;
  37. cout<<"请输入数字B"<<endl;
  38. cin>>b;
  39. intA=atoi(a.c_str());
  40. intB=atoi(b.c_str());
  41. result=operation.GetResult(A,B,op);
  42. cout<<"result="<<result<<endl;
  43. }
  44. catch(exception&e)
  45. {
  46. cout<<"Exception:"<<e.what()<<endl;
  47. }
  48. return0;
  49. }
试想,如果想增加一个sqrt()。是不是还是要阅读源代码,然后在相应位置添加实现sqrt()的源代码,才可以实现。

如何才能实现松耦合呢?

采用面向对象技术的继承就可以。

菜鸟进阶二


  1. #include<iostream>
  2. #include<exception>
  3. usingnamespacestd;
  4. classOperation//操作运算基类
  5. {
  6. public:
  7. Operation(){}
  8. ~Operation(){}
  9. public:
  10. doublenumberA;
  11. doublenumberB;
  12. charoperate;
  13. public:
  14. voidsetNumberA(doublenumber)//设置第一个操作数
  15. {
  16. numberA=number;
  17. }
  18. doublegetNumberA()//获取第一个操作数
  19. {
  20. returnnumberA;
  21. }
  22. voidsetNumberB(doublenumber)
  23. {
  24. numberB=number;
  25. }
  26. doublegetNumberB()
  27. {
  28. returnnumberB;
  29. }
  30. public:
  31. virtualdoubleGetResult()//虚拟类
  32. {
  33. doubleresult=0;
  34. returnresult;
  35. }
  36. };
  37. classOperationAdd:publicOperation
  38. {
  39. public:
  40. OperationAdd(void){}
  41. ~OperationAdd(void){}
  42. public:
  43. doubleGetResult()//重载基类中方法,实现加法
  44. {
  45. doubleresult=0;
  46. result=numberA+numberB;
  47. returnresult;
  48. }
  49. };
  50. classOperationSub:publicOperation
  51. {
  52. public:
  53. OperationSub(void){}
  54. ~OperationSub(void){}
  55. public:
  56. doubleGetResult()//重载基类中方法,实现减法
  57. {
  58. doubleresult=0;
  59. result=numberA-numberB;
  60. returnresult;
  61. }
  62. };
  63. classOperationMul:publicOperation
  64. {
  65. public:
  66. OperationMul(void){}
  67. ~OperationMul(void){}
  68. public:
  69. doubleGetResult()//重载基类中方法,实现乘法
  70. {
  71. doubleresult=0;
  72. result=numberA*numberB;
  73. returnresult;
  74. }
  75. };
  76. classOperationDiv:publicOperation
  77. {
  78. public:
  79. OperationDiv(void){}
  80. ~OperationDiv(void){}
  81. public:
  82. doubleGetResult()//重载基类中方法,实现除法
  83. {
  84. doubleresult=0;
  85. result=numberA/numberB;
  86. returnresult;
  87. }
  88. };
  89. classOperationFactory//运算工厂类
  90. {
  91. public:
  92. OperationFactory(){}
  93. ~OperationFactory(){}
  94. public:
  95. Operation*oper;//运算基类可以生成不同运算的子类(+,-,*,/)
  96. public:
  97. Operation*createOperate(charoperate)//返回类型是指针
  98. {
  99. switch(operate)
  100. {
  101. case'+':
  102. {
  103. oper=newOperationAdd();
  104. break;
  105. }
  106. case'-':
  107. {
  108. oper=newOperationSub();
  109. break;
  110. }
  111. case'*':
  112. {
  113. oper=newOperationMul();
  114. break;
  115. }
  116. case'/':
  117. {
  118. oper=newOperationDiv();
  119. break;
  120. }
  121. }
  122. returnoper;
  123. }
  124. };
  125. intmain()
  126. {
  127. doublestrNumberA,strNumberB,result;
  128. charstrOperation;
  129. Operation*oper;
  130. OperationFactoryfactory;
  131. oper=factory.createOperate('*');
  132. oper->setNumberA(2);
  133. oper->setNumberB(4);
  134. result=oper->GetResult();//调用的运算工厂返回的,各个操作运算符生成的对象
  135. cout<<"result="<<result<<endl;
  136. return0;
  137. }

整个程序结构说明:

class Operation //操作运算基类 ,包含运算所必须的操作运算符和操作数,提供设定操作数和返回操作数方法

class OperationAdd :public Operation //加法运算子类,设定运算结果

class OperationSub :public Operation //减法运算子类

class OperationMul :public Operation //乘法运算子类

class OperationDiv :public Operation //除法运算子类

class OperationFactory //运算工厂类 ,负责生成各个操作运算符的类。


【注意】Operation *createOperate(char operate) //返回类型是指针



主要构成结构为: 操作运算符基类:定义操作必须的运算符和操作数,提供设定操作数和返回操作数方法

四种运算子类:继承操作运算符基类,并实现每种运算符下的结果

运算工厂:根据传递参数,生成不同运算符子类返回相应结果

客户端:建立操作符基类,根据工厂生成子类,然后返回结果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值