zoukankan      html  css  js  c++  java
  • 适配器模式-Adapter

    1.简介

     适配器模式,将一个接口转换成所期待的另外一个接口。这种模式特点,依赖抽象,而不依赖具体。适配器是一个抽象层,用户接口依赖于适配器(抽象层),而不依赖于具体的实现类。当具体类变化时,通过适配器重新转换接口,从而使用代码保持稳定。

     

    unit Unit4;

    interface

    uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
    System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

    type
    ITarget = interface(IInterface) //目标使用的特定接口
    ['{58D98DF5-F85A-430D-84EC-37AA5BFB0EC2}']
    procedure Request;
    end;

    Tadaptee = class(TInterfacedObject)//被适配者,需要匹配的接口
    public
    procedure SpecificRequest;
    end;

    TAdapter = class(Tadaptee, ITarget) //适配器,将TAdaptee接口转换成ITarget的接口
    public
    procedure Request;
    end;

    TForm4 = class(TForm)
    procedure FormCreate(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form4: TForm4;

    implementation

    {$R *.dfm}

    { TAdapter }

    procedure TAdapter.Request;
    begin
    //适配器进行方法匹配的代码
    SpecificRequest;
    end;

    { Tadaptee }

    procedure Tadaptee.SpecificRequest;
    begin
    //被适配类的源方法代码
    end;

    end.

     

    //学习来源于刘艺老师的《模式编程》

    以下是具体实现

    unit Unit4;

    interface

    uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
    System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
    Vcl.StdCtrls;

    type
    ICarTarget = interface(IInterface) //目标使用的特定接口
    ['{58D98DF5-F85A-430D-84EC-37AA5BFB0EC2}']
    procedure Request;
    end;

    TadapteeA = class(TInterfacedObject)//被适配者A
    public
    procedure SpecificRequest;
    end;

    TadapteeB = class(TInterfacedObject)//被适配者B
    public
    procedure SpecificRequest;
    end;

    TAdapterA= class(TadapteeA, ICarTarget) //适配器,将TAdapteeA接口转换成ITarget的接口
    public
    procedure Request;
    end;

    TAdapterB= class(TadapteeB, ICarTarget) //适配器,将TAdapteeB接口转换成ITarget的接口
    public
    procedure Request;
    end;

    TForm4 = class(TForm)
    btn1: TButton;
    btn2: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form4: TForm4;

    implementation

    {$R *.dfm}

    { TAdapter }

    procedure TAdapterA.Request;
    begin
    SpecificRequest;
    end;

    { Tadaptee }

    procedure TadapteeA.SpecificRequest;
    begin
    ShowMessage('转换的是TadapteeA');
    end;

    { TAdapterB }

    procedure TAdapterB.Request;
    begin
    SpecificRequest;
    end;

    { TadapteeB }

    procedure TadapteeB.SpecificRequest;
    begin
    ShowMessage('转换的是TadapteeB');
    end;

    procedure TForm4.btn1Click(Sender: TObject);
    var _AdapterA:ICarTarget;
    begin
    _AdapterA:=TAdapterA.Create;
    _AdapterA.Request;
    end;

    procedure TForm4.btn2Click(Sender: TObject);
    var _AdapterB:ICarTarget;
    begin
    _AdapterB:=TAdapterB.Create;
    _AdapterB.Request;
    end;

    end.

    总结,起初不怎么搞的清楚具体作用,后面百度看了其它博主写的实现,感觉并不符合“适配器”这个名称,他们大部分是定义一个接口或抽象类,然后不同的类继承并覆盖其方法,个人认为严谨的说不应该算“适配器模式”。适配器模式

    其实就是先定义一个抽象层,将现有的类进行转换,就才是真真的适配器作用。具体描述参看《模式编程》.这种模式优点是显而易见的,但缺点也很明显那就是执行效率相对低下。本来也在学习中,有不同意见的请指正!

  • 相关阅读:
    「codeforces
    「sdoi2019
    「ABC 218」解集
    「hdu
    「atcoder
    「tricks」平凡二分幻术
    并查集
    Bellman-Ford算法 & SPFA & SPFA_DFS
    最近公共祖先(LCA)
    题解 P5751 【[NOI1999]01串】
  • 原文地址:https://www.cnblogs.com/carcode/p/12500481.html
Copyright © 2011-2022 走看看