Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---适配器模式之TurkeyAdapter[转]

2020-12-13 15:42

阅读:310

标签:des   blog   http   io   ar   for   sp   div   on   

      适配器模式的主要意图是对现有类的接口进行转换,以满足目标类的需求。其次,可以给目标类的接口添加新的行为(主要指方法)。这一点容易与装饰模式混淆。从意图方面来看,装饰模式不改变(通常指增加)接口中的行为(主要指方法),而是在原有行为(主要指方法)的基础上添加新的功能;从类结构方面来看,装饰模式中的装饰者既继承又组合被装饰者。类适配器同时继承现有类和目标类,对象适配器继承目标类组合现有类。

 

soscw.com,搜素材
 1soscw.com,搜素材
 2soscw.com,搜素材{《HeadFirst设计模式》之适配器模式 }
 3soscw.com,搜素材{ 现有类                         }
 4soscw.com,搜素材{ 编译工具:Delphi7.0             }
 5soscw.com,搜素材{ E-Mail :guzh-0417@163.com     } 
 6soscw.com,搜素材
 7soscw.com,搜素材unit uAdaptee;
 8soscw.com,搜素材
 9soscw.com,搜素材interface
10soscw.com,搜素材
11soscw.com,搜素材type
12soscw.com,搜素材  TTurkey = class(TObject)
13soscw.com,搜素材  public
14soscw.com,搜素材    procedure Gobble; virtual; abstract;
15soscw.com,搜素材    procedure Fly;    virtual; abstract;
16soscw.com,搜素材  end;
17soscw.com,搜素材
18soscw.com,搜素材  TWildTurkey = class(TTurkey)
19soscw.com,搜素材  public
20soscw.com,搜素材    procedure Gobble; override;
21soscw.com,搜素材    procedure Fly;    override;
22soscw.com,搜素材  end;
23soscw.com,搜素材
24soscw.com,搜素材implementation
25soscw.com,搜素材
26soscw.com,搜素材{ TWildTurkey }
27soscw.com,搜素材
28soscw.com,搜素材procedure TWildTurkey.Fly;
29soscw.com,搜素材begin
30soscw.com,搜素材  Writeln(‘I‘‘m flying a short distance.‘);
31soscw.com,搜素材end;
32soscw.com,搜素材
33soscw.com,搜素材procedure TWildTurkey.Gobble;
34soscw.com,搜素材begin
35soscw.com,搜素材  Writeln(‘Gobble gobble.‘);
36soscw.com,搜素材end;
37soscw.com,搜素材
38soscw.com,搜素材end.

      

soscw.com,搜素材
 1soscw.com,搜素材
 2soscw.com,搜素材{《HeadFirst设计模式》之适配器模式 }
 3soscw.com,搜素材{ 目标类                         }
 4soscw.com,搜素材{ 编译工具:Delphi7.0             }
 5soscw.com,搜素材{ E-Mail :guzh-0417@163.com     }
 6soscw.com,搜素材
 7soscw.com,搜素材unit uTarget;
 8soscw.com,搜素材
 9soscw.com,搜素材interface
10soscw.com,搜素材
11soscw.com,搜素材type
12soscw.com,搜素材  TDuck = class(TObject)
13soscw.com,搜素材  public
14soscw.com,搜素材    procedure Quack; virtual; abstract;
15soscw.com,搜素材    procedure Fly;   virtual; abstract;
16soscw.com,搜素材  end;
17soscw.com,搜素材
18soscw.com,搜素材  TMallarDuck = class(TDuck)
19soscw.com,搜素材  public
20soscw.com,搜素材    procedure Quack; override;
21soscw.com,搜素材    procedure Fly;   override;
22soscw.com,搜素材  end;
23soscw.com,搜素材
24soscw.com,搜素材implementation
25soscw.com,搜素材
26soscw.com,搜素材{ TMallarDuck }
27soscw.com,搜素材
28soscw.com,搜素材procedure TMallarDuck.Fly;
29soscw.com,搜素材begin
30soscw.com,搜素材  Writeln(‘I‘‘m flying.‘);
31soscw.com,搜素材end;
32soscw.com,搜素材
33soscw.com,搜素材procedure TMallarDuck.Quack;
34soscw.com,搜素材begin
35soscw.com,搜素材  Writeln(‘Quack.‘);
36soscw.com,搜素材end;
37soscw.com,搜素材
38soscw.com,搜素材end.

 

soscw.com,搜素材
 1soscw.com,搜素材
 2soscw.com,搜素材{《HeadFirst设计模式》之适配器模式 }
 3soscw.com,搜素材{ 适配器,转换现有类的接口         }
 4soscw.com,搜素材{ 编译工具:Delphi7.0            }
 5soscw.com,搜素材{ E-Mail :guzh-0417@163.com    }
 6soscw.com,搜素材
 7soscw.com,搜素材unit uAdapter;
 8soscw.com,搜素材
 9soscw.com,搜素材interface
10soscw.com,搜素材
11soscw.com,搜素材uses
12soscw.com,搜素材  uTarget, uAdaptee;
13soscw.com,搜素材
14soscw.com,搜素材type
15soscw.com,搜素材  TTurkeyAdapter = class(TDuck)
16soscw.com,搜素材  private
17soscw.com,搜素材    FTurkey: TTurkey;
18soscw.com,搜素材  public
19soscw.com,搜素材    constructor Create(aTurkey: TTurkey);
20soscw.com,搜素材    procedure Quack; override;
21soscw.com,搜素材    procedure Fly;   override;
22soscw.com,搜素材  end;
23soscw.com,搜素材
24soscw.com,搜素材implementation
25soscw.com,搜素材
26soscw.com,搜素材{ TTurkeyAdapter }
27soscw.com,搜素材
28soscw.com,搜素材constructor TTurkeyAdapter.Create(aTurkey: TTurkey);
29soscw.com,搜素材begin
30soscw.com,搜素材  FTurkey := aTurkey;
31soscw.com,搜素材end;
32soscw.com,搜素材
33soscw.com,搜素材procedure TTurkeyAdapter.Fly;
34soscw.com,搜素材var
35soscw.com,搜素材  i: Integer;
36soscw.com,搜素材begin
37soscw.com,搜素材  for i := 0 to 4 do
38soscw.com,搜素材  begin
39soscw.com,搜素材    FTurkey.Fly;
40soscw.com,搜素材  end;
41soscw.com,搜素材end;
42soscw.com,搜素材
43soscw.com,搜素材procedure TTurkeyAdapter.Quack;
44soscw.com,搜素材begin
45soscw.com,搜素材  FTurkey.Gobble;
46soscw.com,搜素材end;
47soscw.com,搜素材
48soscw.com,搜素材end.

 

soscw.com,搜素材
 1soscw.com,搜素材
 2soscw.com,搜素材{《HeadFirst设计模式》之适配器模式 }
 3soscw.com,搜素材{ 客户端                         }
 4soscw.com,搜素材{ 编译工具:Delphi7.0             }
 5soscw.com,搜素材{ E-Mail :guzh-0417@163.com     }
 6soscw.com,搜素材
 7soscw.com,搜素材program pDuckTestDrive;
 8soscw.com,搜素材
 9soscw.com,搜素材{$APPTYPE CONSOLE}
10soscw.com,搜素材
11soscw.com,搜素材uses
12soscw.com,搜素材  SysUtils,
13soscw.com,搜素材  uAdaptee in ‘uAdaptee.pas‘,
14soscw.com,搜素材  uAdapter in ‘uAdapter.pas‘,
15soscw.com,搜素材  uTarget in ‘uTarget.pas‘;
16soscw.com,搜素材  
17soscw.com,搜素材var
18soscw.com,搜素材  Duck  : TMallarDuck;
19soscw.com,搜素材  Turkey: TWildTurkey;
20soscw.com,搜素材  TurkeyAdapter: TTurkeyAdapter;
21soscw.com,搜素材
22soscw.com,搜素材procedure TestDuck(aDuck: TDuck);
23soscw.com,搜素材begin
24soscw.com,搜素材  aDuck.Quack;
25soscw.com,搜素材  aDuck.Fly;
26soscw.com,搜素材end;
27soscw.com,搜素材
28soscw.com,搜素材begin
29soscw.com,搜素材  Duck   := TMallarDuck.Create;
30soscw.com,搜素材  Turkey := TWildTurkey.Create;
31soscw.com,搜素材  TurkeyAdapter := TTurkeyAdapter.Create(Turkey);
32soscw.com,搜素材
33soscw.com,搜素材  Writeln(‘The turkey says:‘);
34soscw.com,搜素材  Turkey.Gobble;
35soscw.com,搜素材  Turkey.Fly;
36soscw.com,搜素材
37soscw.com,搜素材  Writeln;
38soscw.com,搜素材
39soscw.com,搜素材  Writeln(‘The duck says:‘);
40soscw.com,搜素材  TestDuck(Duck);
41soscw.com,搜素材
42soscw.com,搜素材  Writeln;
43soscw.com,搜素材
44soscw.com,搜素材  Writeln(‘The turkeyAdapter says:‘);
45soscw.com,搜素材  TestDuck(TurkeyAdapter);
46soscw.com,搜素材
47soscw.com,搜素材  FreeAndNil(Duck);
48soscw.com,搜素材  FreeAndNil(Turkey);
49soscw.com,搜素材  FreeAndNil(TurkeyAdapter);
50soscw.com,搜素材
51soscw.com,搜素材  Readln;
52soscw.com,搜素材end.

 

运行结果:

soscw.com,搜素材

 

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---适配器模式之TurkeyAdapter[转]

标签:des   blog   http   io   ar   for   sp   div   on   

原文地址:http://www.cnblogs.com/0x2D-0x22/p/4076307.html


评论


亲,登录后才可以留言!