zoukankan      html  css  js  c++  java
  • 类继承复用之适配器模式

    以类的适配器模式为例:
    type
      ITarget=interface//目标
        procedure Hello;
        Procedure Hello2;
      end;

      TAdaptee=class(TInterfacedObject)//被适配者一
      public
        procedure Say;
      end;

      TAdaptee2=class(TInterfacedObject)//被适配者二
      public
        procedure Tell;
      end;

      TAdapter=class(TAdaptee,ITarget)//适配器(多重继承)
      public
        procedure Hello;
        procedure Hello2;
      end;

    implementation
     
    procedure TAdaptee.Say;
    begin
      //do something
    end;

    procedure TAdaptee.Tell;
    begin
      //do something
    end;

    procedure TAdapter.Hello;
    begin
      Say;//转换接口
    end;

    procedure TAdapter.Hello2;
    begin
      Tell;//转换接口
    end;

    这样,TAdapter类就能复用TAdaptee的Say方法和TAdaptee2的Tell方法,并将这两个接口分别转换成Hello和Hello2,达到将第三方API接口转

    换成与自己现有的接口兼容的目的。其核心是多重继承。  
  • 相关阅读:
    [HNOI2008] [BZOJ1008] 越狱|组合数学
    (转)位运算简介及使用技巧
    AW297 赤壁之战(数据结构优化DP)
    AW280 陪审团
    AW288 休息时间
    AW281 硬币
    AW383 观光
    AW366 看牛 (欧拉回路)
    AW365 圆桌骑士
    AW363 B城
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2941057.html
Copyright © 2011-2022 走看看