Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---适配器模式之TurkeyAdapter
Posted on 2009-06-03 10:52 都市牧羊 阅读(608) 评论(0) 编辑 收藏 举报
适配器模式的主要意图是对现有类的接口进行转换,以满足目标类的需求。其次,可以给目标类的接口添加新的行为(主要指方法)。这一点容易与装饰模式混淆。从意图方面来看,装饰模式不改变(通常指增加)接口中的行为(主要指方法),而是在原有行为(主要指方法)的基础上添加新的功能;从类结构方面来看,装饰模式中的装饰者既继承又组合被装饰者。类适配器同时继承现有类和目标类,对象适配器继承目标类组合现有类。
现有类,被转换接口者
1
2{《HeadFirst设计模式》之适配器模式 }
3{ 现有类 }
4{ 编译工具:Delphi7.0 }
5{ E-Mail :guzh-0417@163.com }
6
7unit uAdaptee;
8
9interface
10
11type
12 TTurkey = class(TObject)
13 public
14 procedure Gobble; virtual; abstract;
15 procedure Fly; virtual; abstract;
16 end;
17
18 TWildTurkey = class(TTurkey)
19 public
20 procedure Gobble; override;
21 procedure Fly; override;
22 end;
23
24implementation
25
26{ TWildTurkey }
27
28procedure TWildTurkey.Fly;
29begin
30 Writeln('I''m flying a short distance.');
31end;
32
33procedure TWildTurkey.Gobble;
34begin
35 Writeln('Gobble gobble.');
36end;
37
38end.
1
2{《HeadFirst设计模式》之适配器模式 }
3{ 现有类 }
4{ 编译工具:Delphi7.0 }
5{ E-Mail :guzh-0417@163.com }
6
7unit uAdaptee;
8
9interface
10
11type
12 TTurkey = class(TObject)
13 public
14 procedure Gobble; virtual; abstract;
15 procedure Fly; virtual; abstract;
16 end;
17
18 TWildTurkey = class(TTurkey)
19 public
20 procedure Gobble; override;
21 procedure Fly; override;
22 end;
23
24implementation
25
26{ TWildTurkey }
27
28procedure TWildTurkey.Fly;
29begin
30 Writeln('I''m flying a short distance.');
31end;
32
33procedure TWildTurkey.Gobble;
34begin
35 Writeln('Gobble gobble.');
36end;
37
38end.
目标类
1
2{《HeadFirst设计模式》之适配器模式 }
3{ 目标类 }
4{ 编译工具:Delphi7.0 }
5{ E-Mail :guzh-0417@163.com }
6
7unit uTarget;
8
9interface
10
11type
12 TDuck = class(TObject)
13 public
14 procedure Quack; virtual; abstract;
15 procedure Fly; virtual; abstract;
16 end;
17
18 TMallarDuck = class(TDuck)
19 public
20 procedure Quack; override;
21 procedure Fly; override;
22 end;
23
24implementation
25
26{ TMallarDuck }
27
28procedure TMallarDuck.Fly;
29begin
30 Writeln('I''m flying.');
31end;
32
33procedure TMallarDuck.Quack;
34begin
35 Writeln('Quack.');
36end;
37
38end.
1
2{《HeadFirst设计模式》之适配器模式 }
3{ 目标类 }
4{ 编译工具:Delphi7.0 }
5{ E-Mail :guzh-0417@163.com }
6
7unit uTarget;
8
9interface
10
11type
12 TDuck = class(TObject)
13 public
14 procedure Quack; virtual; abstract;
15 procedure Fly; virtual; abstract;
16 end;
17
18 TMallarDuck = class(TDuck)
19 public
20 procedure Quack; override;
21 procedure Fly; override;
22 end;
23
24implementation
25
26{ TMallarDuck }
27
28procedure TMallarDuck.Fly;
29begin
30 Writeln('I''m flying.');
31end;
32
33procedure TMallarDuck.Quack;
34begin
35 Writeln('Quack.');
36end;
37
38end.
适配器
1
2{《HeadFirst设计模式》之适配器模式 }
3{ 适配器,转换现有类的接口 }
4{ 编译工具:Delphi7.0 }
5{ E-Mail :guzh-0417@163.com }
6
7unit uAdapter;
8
9interface
10
11uses
12 uTarget, uAdaptee;
13
14type
15 TTurkeyAdapter = class(TDuck)
16 private
17 FTurkey: TTurkey;
18 public
19 constructor Create(aTurkey: TTurkey);
20 procedure Quack; override;
21 procedure Fly; override;
22 end;
23
24implementation
25
26{ TTurkeyAdapter }
27
28constructor TTurkeyAdapter.Create(aTurkey: TTurkey);
29begin
30 FTurkey := aTurkey;
31end;
32
33procedure TTurkeyAdapter.Fly;
34var
35 i: Integer;
36begin
37 for i := 0 to 4 do
38 begin
39 FTurkey.Fly;
40 end;
41end;
42
43procedure TTurkeyAdapter.Quack;
44begin
45 FTurkey.Gobble;
46end;
47
48end.
1
2{《HeadFirst设计模式》之适配器模式 }
3{ 适配器,转换现有类的接口 }
4{ 编译工具:Delphi7.0 }
5{ E-Mail :guzh-0417@163.com }
6
7unit uAdapter;
8
9interface
10
11uses
12 uTarget, uAdaptee;
13
14type
15 TTurkeyAdapter = class(TDuck)
16 private
17 FTurkey: TTurkey;
18 public
19 constructor Create(aTurkey: TTurkey);
20 procedure Quack; override;
21 procedure Fly; override;
22 end;
23
24implementation
25
26{ TTurkeyAdapter }
27
28constructor TTurkeyAdapter.Create(aTurkey: TTurkey);
29begin
30 FTurkey := aTurkey;
31end;
32
33procedure TTurkeyAdapter.Fly;
34var
35 i: Integer;
36begin
37 for i := 0 to 4 do
38 begin
39 FTurkey.Fly;
40 end;
41end;
42
43procedure TTurkeyAdapter.Quack;
44begin
45 FTurkey.Gobble;
46end;
47
48end.
客户端
1
2{《HeadFirst设计模式》之适配器模式 }
3{ 客户端 }
4{ 编译工具:Delphi7.0 }
5{ E-Mail :guzh-0417@163.com }
6
7program pDuckTestDrive;
8
9{$APPTYPE CONSOLE}
10
11uses
12 SysUtils,
13 uAdaptee in 'uAdaptee.pas',
14 uAdapter in 'uAdapter.pas',
15 uTarget in 'uTarget.pas';
16
17var
18 Duck : TMallarDuck;
19 Turkey: TWildTurkey;
20 TurkeyAdapter: TTurkeyAdapter;
21
22procedure TestDuck(aDuck: TDuck);
23begin
24 aDuck.Quack;
25 aDuck.Fly;
26end;
27
28begin
29 Duck := TMallarDuck.Create;
30 Turkey := TWildTurkey.Create;
31 TurkeyAdapter := TTurkeyAdapter.Create(Turkey);
32
33 Writeln('The turkey says:');
34 Turkey.Gobble;
35 Turkey.Fly;
36
37 Writeln;
38
39 Writeln('The duck says:');
40 TestDuck(Duck);
41
42 Writeln;
43
44 Writeln('The turkeyAdapter says:');
45 TestDuck(TurkeyAdapter);
46
47 FreeAndNil(Duck);
48 FreeAndNil(Turkey);
49 FreeAndNil(TurkeyAdapter);
50
51 Readln;
52end.
1
2{《HeadFirst设计模式》之适配器模式 }
3{ 客户端 }
4{ 编译工具:Delphi7.0 }
5{ E-Mail :guzh-0417@163.com }
6
7program pDuckTestDrive;
8
9{$APPTYPE CONSOLE}
10
11uses
12 SysUtils,
13 uAdaptee in 'uAdaptee.pas',
14 uAdapter in 'uAdapter.pas',
15 uTarget in 'uTarget.pas';
16
17var
18 Duck : TMallarDuck;
19 Turkey: TWildTurkey;
20 TurkeyAdapter: TTurkeyAdapter;
21
22procedure TestDuck(aDuck: TDuck);
23begin
24 aDuck.Quack;
25 aDuck.Fly;
26end;
27
28begin
29 Duck := TMallarDuck.Create;
30 Turkey := TWildTurkey.Create;
31 TurkeyAdapter := TTurkeyAdapter.Create(Turkey);
32
33 Writeln('The turkey says:');
34 Turkey.Gobble;
35 Turkey.Fly;
36
37 Writeln;
38
39 Writeln('The duck says:');
40 TestDuck(Duck);
41
42 Writeln;
43
44 Writeln('The turkeyAdapter says:');
45 TestDuck(TurkeyAdapter);
46
47 FreeAndNil(Duck);
48 FreeAndNil(Turkey);
49 FreeAndNil(TurkeyAdapter);
50
51 Readln;
52end.
运行结果: