zoukankan      html  css  js  c++  java
  • C++模式学习------代理模式

    Proxy代理模式 :

    为其他对象提供一种代理以控制对这个对象的访问。代理类作为桥梁是请求方和执行方的中间者,将请求方和真正的执行方分割开来,也是两者之间调用的协调者。例如执行类也就是被代理类,可以在不考虑各种特殊条件下提供所有的执行方法,请求者也可以不考虑特殊条件提出所有的请求。而在调用和被调用时需要考虑的所有条件,在代理类中完成。

    1. 考虑请求方的情况:由各代理处理不同的请求

     1 class Service
     2 {
     3 public:
     4     Service();
     5 
     6     virtual void serviceLv0() = 0;
     7     virtual void serviceLv1() = 0;
     8     virtual void serviceLv2() = 0;
     9 };
    10 
    11 class OfferService : public Service
    12         //真正的服务提供者
    13 {
    14 public:
    15     OfferService();
    16 
    17     void serviceLv0()
    18     {
    19         cout<<"Offer serviceLv0"<<endl;
    20     }
    21     void serviceLv1()
    22     {
    23         cout<<"Offer serviceLv0"<<endl;
    24     }
    25     void serviceLv2()
    26     {
    27         cout<<"Offer serviceLv0"<<endl;
    28     }
    29 };
    30 
    31 class ProxyOfferServiceLv0 : public Service
    32         //lv0 的服务提供代理
    33 {
    34 public:
    35     ProxyOfferServiceLv0()
    36     {
    37         m_instance = new OfferService();
    38     }
    39 
    40     void serviceLv0()
    41     {
    42         m_instance->serviceLv0();
    43     }
    44     void serviceLv1()
    45     {
    46         cout<<"No serviceLv0"<<endl;
    47     }
    48     void serviceLv2()
    49     {
    50         cout<<"No serviceLv0"<<endl;
    51     }
    52 
    53     OfferService* m_instance;
    54 };
    55 
    56 class ProxyOfferServiceLv2 : public Service
    57         //lv2 的服务提供代理
    58 {
    59 public:
    60     ProxyOfferServiceLv2()
    61     {
    62         m_instance = new OfferService();
    63     }
    64 
    65     void serviceLv0()
    66     {
    67         m_instance->serviceLv0();
    68     }
    69     void serviceLv1()
    70     {
    71         m_instance->serviceLv1();
    72     }
    73     void serviceLv2()
    74     {
    75         m_instance->serviceLv2();
    76     }
    77 
    78     OfferService* m_instance;
    79 };

    2. 考虑执行方的情况

      1 enum EnumState
      2 {
      3     State_isOK,
      4     State_notOK
      5 };
      6 
      7 class Service
      8 {
      9 public:
     10     Service();
     11 
     12     virtual void serviceLv0() = 0;
     13     virtual void serviceLv1() = 0;
     14     virtual void serviceLv2() = 0;
     15 
     16     int getServiceState()
     17     {
     18         return m_state;
     19     }
     20 
     21     int m_state; // EnumState
     22 };
     23 
     24 class OfferService : public Service
     25         //真正的服务提供者
     26 {
     27 public:
     28     OfferService();
     29 
     30     void serviceLv0()
     31     {
     32         cout<<"Offer serviceLv0"<<endl;
     33     }
     34     void serviceLv1()
     35     {
     36         cout<<"Offer serviceLv0"<<endl;
     37     }
     38     void serviceLv2()
     39     {
     40         cout<<"Offer serviceLv0"<<endl;
     41     }
     42 };
     43 
     44 class ProxyOfferServiceLv0 : public Service
     45         //lv0 的服务提供代理
     46 {
     47 public:
     48     ProxyOfferServiceLv0()
     49     {
     50         m_instance = new OfferService();
     51     }
     52 
     53     void serviceLv0()
     54     {
     55         if(m_instance->getServiceState() == State_isOK)
     56             //查看执行方的状态
     57         {
     58             m_instance->serviceLv0();
     59         }
     60         else
     61         {
     62             cout<<"serviceLv0 is noOK"<<endl;
     63         }
     64     }
     65     void serviceLv1()
     66     {
     67         cout<<"No serviceLv0"<<endl;
     68     }
     69     void serviceLv2()
     70     {
     71         cout<<"No serviceLv0"<<endl;
     72     }
     73 
     74     OfferService* m_instance;
     75 };
     76 
     77 class ProxyOfferServiceLv2 : public Service
     78         //lv2 的服务提供代理
     79 {
     80 public:
     81     ProxyOfferServiceLv2()
     82     {
     83         m_instance = new OfferService();
     84     }
     85 
     86     void serviceLv0()
     87     {
     88         if(m_instance->getServiceState() == State_isOK)
     89             //查看执行方的状态
     90         {
     91             m_instance->serviceLv0();
     92         }
     93         else
     94         {
     95             cout<<"serviceLv0 is noOK"<<endl;
     96         }
     97     }
     98     void serviceLv1()
     99     {
    100         if(m_instance->getServiceState() == State_isOK)
    101             //查看执行方的状态
    102         {
    103             m_instance->serviceLv1();
    104         }
    105         else
    106         {
    107             cout<<"serviceLv1 is noOK"<<endl;
    108         }
    109     }
    110     void serviceLv2()
    111     {
    112         if(m_instance->getServiceState() == State_isOK)
    113             //查看执行方的状态
    114         {
    115             m_instance->serviceLv2();
    116         }
    117         else
    118         {
    119             cout<<"serviceLv2 is noOK"<<endl;
    120         }
    121     }
    122 
    123     OfferService* m_instance;
    124 };
  • 相关阅读:
    [AU3]技巧
    [AU3]Windows 10 Update message DETELEDER Win10更新通知删除助手
    vue多选框选择后显示选中的内容
    计算object的长度
    关于苹果手机点击事件无效的解决办法
    vue中将汉字按照首字母排序,也适用于其他地方,但不适用多音字
    关于json数据格式错误
    Java下使用Swing来进行图形界面开发
    数字与静态
    Java中的内存机制及管理
  • 原文地址:https://www.cnblogs.com/tyche116/p/8671232.html
Copyright © 2011-2022 走看看