zoukankan      html  css  js  c++  java
  • adapter模式

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class target
     5 {
     6 public:
     7     target(){}
     8     virtual ~target(){}
     9 
    10     virtual void Operation(){};
    11 };
    12 
    13 class adaptee{
    14 public:
    15     void special_work(){
    16         cout<<"only I can do!!\n";
    17     }
    18 };
    19 
    20 
    21 /*类适配器模式
    22  * 优点:可以重定义adaptee的部分行为
    23  * 缺点:不能匹配adaptee的所有子类*/
    24 class adapter: public target,private adaptee
    25 {
    26 public:
    27     virtual void Operation(){adaptee::special_work();}
    28 };
    29 
    30 int main()
    31 {
    32     target* tmp=new adapter();
    33     tmp->Operation();
    34     return 0;
    35 }
     1 #include <iostream>
     2 using namespace std;
     3 
     4 class target
     5 {
     6 public:
     7     target(){}
     8     virtual ~target(){}
     9 
    10     virtual void Operation(){};
    11 };
    12 
    13 class adaptee{
    14 public:
    15     void special_work(){
    16         cout<<"only I can do!!\n";
    17     }
    18 };
    19 
    20 
    21 /*对象适配器模式
    22  * 缺点:很难重新定义adaptee的行为,只能通过生成子类
    23  * 优点:一个adapter可以匹配adaptee的所有子类*/
    24 class adapter: public target
    25 {
    26     adaptee* p_adaptee;
    27 public:
    28     adapter(adaptee* var){
    29         p_adaptee=var;
    30     }
    31     virtual void Operation(){p_adaptee->special_work();}
    32 };
    33 
    34 int main()
    35 {
    36     adaptee* tmp_adaptee=new adaptee();
    37     target* tmp=new adapter(tmp_adaptee);
    38     tmp->Operation();
    39     return 0;
    40 }
  • 相关阅读:
    CF1175B Catch Overflow!
    震惊!一蒟蒻竟然写出fhqTreap
    树上差分
    洛谷 P3128 最大流Max Flow
    线段树的标记永久化/二维线段树模板
    矩阵加速~desire drive
    置换相关
    树形图们
    严格单调递增与非严格之间的转换
    记录延续性的一类dp
  • 原文地址:https://www.cnblogs.com/kakamilan/p/2599062.html
Copyright © 2011-2022 走看看