C++中的策略模式怎么实现

其他教程   发布日期:2023年07月12日   浏览次数:414

本文小编为大家详细介绍“C++中的策略模式怎么实现”,内容详细,步骤清晰,细节处理妥当,希望这篇“C++中的策略模式怎么实现”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

策略模式主要解决在有多种算法相似的情况下,使用 if…else 所带来的复杂和难以维护,其实际就是用来抽象变化的(和开放-封闭原则是一个原理),只要在分析过程中我们发现需要在不同的时间运用不同类型的业务规则或者代码中可能会出现很多变化,就可以考虑使用策略模式来处理这种变化。

策略模式通常的使用方法就是一个抽象策略类,若干具体策略类和一个Context类,同时Conetext类可以结合简单工厂模式让用户与策略类完全解耦,比如可以向Context类的构造函数中传入参数而不是策略类,然后在Conext的构造函数里用简单工厂模式根据传递的参数初始化策略类,甚至还可以什么都不传,定义一个默认策略供用户使用(简单工厂不一定是要一个单独的类)。Conetext类中包含一个策略类的指针指向简单工厂实例化出的具体策略类对象,还包含一个contextDeloy接口用于通过策略类指针去调用实例化出的具体策略类对象的接口,可以让用户面对Context的接口编程,而不与策略类接口直接耦合 ,方便策略类日后更改接口,同时还需要一个get接口,用于获取简单工厂中实例化出的对象。在业务逻辑层,我们先判断简单工厂模式实例化的具体对象是否为空,如果不为空,我们就可以通过contextDeloy接口去访问实例化的具体策略类对象的接口。

接下来我将用策略模式改写之前的计算器5.0版本。

  1. #include<iostream>
  2. using namespace std;
  3. #include<string>
  4. //业务逻辑
  5. //异常类用于处理异常情况
  6. class opeException
  7. {
  8. public:
  9. void getMessage()
  10. {
  11. cout << "您的输入有误!" << endl;
  12. }
  13. };
  14. //运算类
  15. class Operation
  16. {
  17. //判断一个字符串是不是数字
  18. bool isStringNum(string& s)
  19. {
  20. bool flag = true;
  21. for (auto e : s)
  22. if (!(isdigit(e)))
  23. {
  24. flag = false;
  25. break;
  26. }
  27. return flag;
  28. }
  29. protected:
  30. bool isError(string& _strNum1, string& _strNum2, string& _ope)
  31. {
  32. if (!(Operation::isStringNum(_strNum1) && Operation::isStringNum(_strNum2) && (_ope == "+" || _ope == "-" || _ope == "*" || _ope == "/")))
  33. {
  34. return false;
  35. }
  36. }
  37. public:
  38. virtual int getResult()
  39. {
  40. return 0;
  41. }
  42. };
  43. //加法运算类
  44. class addOperation :public Operation
  45. {
  46. private:
  47. string strNum1;
  48. string strNum2;
  49. string ope;
  50. int re;
  51. public:
  52. addOperation(string& _strNum1, string& _strNum2, string& _ope) :strNum1(_strNum1), strNum2(_strNum2), ope(_ope), re(0) {}
  53. virtual int getResult() override
  54. {
  55. if (!isError(strNum1, strNum2, ope))
  56. throw opeException();
  57. else
  58. re = stoi(strNum1) + stoi(strNum2);
  59. return re;
  60. }
  61. };
  62. //减法运算类
  63. class subOperation :public Operation
  64. {
  65. private:
  66. string strNum1;
  67. string strNum2;
  68. string ope;
  69. int re;
  70. public:
  71. subOperation(string& _strNum1, string& _strNum2, string& _ope) :strNum1(_strNum1), strNum2(_strNum2), ope(_ope), re(0) {}
  72. virtual int getResult() override
  73. {
  74. if (!isError(strNum1, strNum2, ope))
  75. throw opeException();
  76. else
  77. re = stoi(strNum1) - stoi(strNum2);
  78. return re;
  79. }
  80. };
  81. //乘法运算类
  82. class mulOperation :public Operation
  83. {
  84. private:
  85. string strNum1;
  86. string strNum2;
  87. string ope;
  88. int re;
  89. public:
  90. mulOperation(string& _strNum1, string& _strNum2, string& _ope) :strNum1(_strNum1), strNum2(_strNum2), ope(_ope), re(0) {}
  91. virtual int getResult() override
  92. {
  93. if (!isError(strNum1, strNum2, ope))
  94. throw opeException();
  95. else
  96. re = stoi(strNum1) * stoi(strNum2);
  97. return re;
  98. }
  99. };
  100. //除法运算类
  101. class divOperation :public Operation
  102. {
  103. private:
  104. string strNum1;
  105. string strNum2;
  106. string ope;
  107. int re;
  108. public:
  109. divOperation(string& _strNum1, string& _strNum2, string& _ope) :strNum1(_strNum1), strNum2(_strNum2), ope(_ope), re(0) {}
  110. virtual int getResult() override
  111. {
  112. if (!isError(strNum1, strNum2, ope))
  113. throw opeException();
  114. else if (stoi(strNum2) != 0)
  115. re = stoi(strNum1) / stoi(strNum2);
  116. else
  117. throw opeException();
  118. return re;
  119. }
  120. };
  121. //Conetext结合简单工厂模式
  122. class Context
  123. {
  124. Operation *operation;
  125. public:
  126. Context(string& _strNum1, string& _strNum2, string& _ope)
  127. {
  128. if (_ope == "+")
  129. {
  130. operation = new addOperation(_strNum1, _strNum2, _ope);
  131. }
  132. else if (_ope == "-")
  133. operation = new subOperation(_strNum1, _strNum2, _ope);
  134. else if (_ope == "*")
  135. operation = new mulOperation(_strNum1, _strNum2, _ope);
  136. else if (_ope == "/")
  137. {
  138. operation = new divOperation(_strNum1, _strNum2, _ope);
  139. }
  140. else
  141. operation = nullptr;
  142. }
  143. Operation* get()
  144. {
  145. return operation;
  146. }
  147. int contextResult()
  148. {
  149. return operation->getResult();
  150. }
  151. };
  152. //界面逻辑
  153. int main()
  154. {
  155. try
  156. {
  157. string _strNum1 = " ";
  158. string _strNum2 = " ";
  159. string _ope = " ";
  160. cout << "请输入左操作数:" << endl;
  161. cin >> _strNum1;
  162. cout << "请输入右操作数:" << endl;
  163. cin >> _strNum2;
  164. cout << "请输入操作符:" << endl;
  165. cin >> _ope;
  166. Context context(_strNum1, _strNum2, _ope);
  167. if (context.get() != nullptr)
  168. cout << context.contextResult() << endl;
  169. else
  170. cout << "您的输入有误!" << endl;
  171. }
  172. catch (opeException ex)
  173. {
  174. cout << "您的输入有误" << endl;
  175. }
  176. return 0;
  177. }

总结策略模式的优缺点:

优点:

1、算法可以自由切换。

2、避免使用多重条件判断。

3、扩展性良好。

缺点:

1、策略类会增多。

2、所有策略类都需要对外暴露。

注意事项:如果一个系统的策略多于四个,就需要考虑使用混合模式,解决策略类膨胀的问题。

以上就是C++中的策略模式怎么实现的详细内容,更多关于C++中的策略模式怎么实现的资料请关注九品源码其它相关文章!