zoukankan      html  css  js  c++  java
  • 《大话设计模式》c++实现 之策略模式

    一、UML图

     

    二、概念

    策略模式:他定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户。

     

    三、优点

    (1)策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,只是实现不同,他可以以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合。

    (2)策略模式的Strategy类曾是为Context定义了一些列的可供重用的算法或行为。集成有助于析取出这些算法中的公共功能。

    (3)策略模式简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试。

    (4)策略模式就是用来封装算法的。

    (5)只要在分析过程中听到需要在不同时间应用不同的业务规则,就可以考虑使用策略模式处理这种变化的可能性。

    (6)简单工厂模式需要让客户端认识两个类,而策略模式和简单工厂模式结合的用法,客户端只需要认识一个类Context即可。

     四 与工厂模式的区别:

    工厂模式用来选择分支,产生不同的产品,(类)

    策略模式是针对算法的,不同的算法用不同的类

      1 #include <iostream>
      2 using namespace std;
      3 
      4 
      5 
      6 
      7 // 抽象产品类
      8 class Operation{
      9 protected:
     10     double num1;
     11     double num2;
     12 public:
     13     double get_n1(){
     14         return num1;
     15     }
     16     double get_n2(){
     17         return num2;
     18     }
     19     void set_n1(int num){
     20         num1 = num;
     21     }
     22     void set_n2(int num){
     23         num2 = num;
     24     }
     25     virtual double GetResult(){
     26         double result = 0;
     27         return result;
     28     }
     29 
     30 };
     31 //具体产品类
     32 class OperationAdd :public Operation{
     33 public:
     34     double GetResult(){
     35         double result = 0;
     36         result = num1 + num2;
     37         return result;
     38     }
     39 };
     40 
     41 //具体产品类
     42 class OperationSub :public Operation{
     43 public:
     44     double GetResult(){
     45         double result = 0;
     46         result = num1 - num2;
     47         return result;
     48     }
     49 };
     50 
     51 //具体产品类
     52 class OperationMul :public Operation{
     53 public:
     54     double GetResult(){
     55         double result = 0;
     56         result = num1 * num2;
     57         return result;
     58     }
     59 };
     60 
     61 //具体产品类
     62 class OperationDiv :public Operation{
     63 public:
     64     double GetResult(){
     65         double result = 0;
     66         if (num2 != 0)
     67             result = num1 / num2;
     68         return result;
     69     }
     70 };
     71 //工厂类
     72 
     73 class OperationFactory{
     74 public:
     75     Operation* createOperation(char type){
     76         Operation* oper = NULL;
     77         switch (type)
     78         {
     79         case '+':
     80             oper = new OperationAdd;
     81             break;
     82         case '-':
     83             oper = new OperationSub;
     84             break;
     85         case '*':
     86             oper = new OperationMul;
     87             break;
     88         case '/':
     89             oper = new OperationDiv;
     90             break;
     91         }
     92         return oper;
     93     }
     94 };
     95 
     96 
     97 //策略类  
     98 class Context{
     99 private:
    100     Operation* op_;
    101 public:
    102     Context(char type){
    103         OperationFactory of;
    104         op_ = of.createOperation(type);
    105     }
    106     double executeOperation(double num1, double num2){
    107         op_->set_n1(num1);
    108         op_->set_n2(num2);
    109         return op_->GetResult();
    110     }
    111     
    112 };
    113 
    114 void main(){
    115 
    116     double res = 0;
    117     Context co = Context('+');
    118     res = co.executeOperation(5, 3);
    119     cout << res << endl;
    120 
    121 
    122     Context co2 = Context('-');
    123     res = co2.executeOperation(5, 3);
    124     cout << res << endl;
    125 
    126 
    127 
    128     system("pause");
    129 }

    参考:

    http://www.runoob.com/design-pattern/strategy-pattern.html

    https://blog.csdn.net/xiqingnian/article/details/41855391

  • 相关阅读:
    ov5640调试过程
    关于OnPaint函数的工作原理(很详细,很实用) [转载]
    机器视觉网络资源
    (转)signaltap II数据用于仿真
    DataSocket简介
    datasocket使用网络传输图像
    共享变量/全局变量/datasocket
    [转载] 任意频率分频原理
    [转]影响FPGA设计中时钟因素的探讨
    生产者和消费者
  • 原文地址:https://www.cnblogs.com/zle1992/p/9742300.html
Copyright © 2011-2022 走看看