Delphi 设计模式:《HeadFirst设计模式》Delphi代码---模式小结之一个叫声接口和几只鸭子[转]

2020-12-13 15:44

阅读:206

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

一、一个叫声接口和几只鸭子

1、从一个叫声接口开始。

 

soscw.com,搜素材
 1soscw.com,搜素材
 2soscw.com,搜素材{《HeadFirst设计模式》Delphi代码之模式小结 }
 3soscw.com,搜素材{ 一个叫声接口                            }
 4soscw.com,搜素材{ 编译工具:Delphi2010 for win32          }
 5soscw.com,搜素材{ E-Mail :guzh-0417@163.com             }
 6soscw.com,搜素材
 7soscw.com,搜素材unit uQuackable;
 8soscw.com,搜素材
 9soscw.com,搜素材interface
10soscw.com,搜素材
11soscw.com,搜素材type
12soscw.com,搜素材  IQuackable = interface(IInterface)
13soscw.com,搜素材    procedure Quack;
14soscw.com,搜素材  end;
15soscw.com,搜素材
16soscw.com,搜素材implementation
17soscw.com,搜素材
18soscw.com,搜素材end.
19soscw.com,搜素材

 

2、几只鸭子实现了该叫声接口。

 

soscw.com,搜素材
 1soscw.com,搜素材
 2soscw.com,搜素材{《HeadFirst设计模式》Delphi代码之模式小结 }
 3soscw.com,搜素材{ 几只鸭子实现了声接口                    }
 4soscw.com,搜素材{ 编译工具:Delphi2010 for win32         }
 5soscw.com,搜素材{ E-Mail :guzh-0417@163.com            }
 6soscw.com,搜素材
 7soscw.com,搜素材unit uDuck;
 8soscw.com,搜素材
 9soscw.com,搜素材interface
10soscw.com,搜素材
11soscw.com,搜素材uses
12soscw.com,搜素材  uQuackable;
13soscw.com,搜素材
14soscw.com,搜素材type
15soscw.com,搜素材  TMallardDuck = class(TInterfacedObject, IQuackable)
16soscw.com,搜素材  public
17soscw.com,搜素材    procedure Quack;
18soscw.com,搜素材  end;
19soscw.com,搜素材
20soscw.com,搜素材  TRedHeadDuck = class(TInterfacedObject, IQuackable)
21soscw.com,搜素材  public
22soscw.com,搜素材    procedure Quack;
23soscw.com,搜素材  end;
24soscw.com,搜素材
25soscw.com,搜素材  TDuckCall = class(TInterfacedObject, IQuackable)
26soscw.com,搜素材  public
27soscw.com,搜素材    procedure Quack;
28soscw.com,搜素材  end;
29soscw.com,搜素材
30soscw.com,搜素材  TRubberDuck = class(TInterfacedObject, IQuackable)
31soscw.com,搜素材  public
32soscw.com,搜素材    procedure Quack;
33soscw.com,搜素材  end;
34soscw.com,搜素材
35soscw.com,搜素材implementation
36soscw.com,搜素材
37soscw.com,搜素材{ TMallardDuck }
38soscw.com,搜素材
39soscw.com,搜素材procedure TMallardDuck.Quack;
40soscw.com,搜素材begin
41soscw.com,搜素材  Writeln(‘Quack‘);
42soscw.com,搜素材end;
43soscw.com,搜素材
44soscw.com,搜素材{ TRedHeadDuck }
45soscw.com,搜素材
46soscw.com,搜素材procedure TRedHeadDuck.Quack;
47soscw.com,搜素材begin
48soscw.com,搜素材  Writeln(‘Quack‘);
49soscw.com,搜素材end;
50soscw.com,搜素材
51soscw.com,搜素材{ TDuckCall }
52soscw.com,搜素材
53soscw.com,搜素材procedure TDuckCall.Quack;
54soscw.com,搜素材begin
55soscw.com,搜素材  Writeln(‘Kwak‘);
56soscw.com,搜素材end;
57soscw.com,搜素材
58soscw.com,搜素材{ TRubberDuck }
59soscw.com,搜素材
60soscw.com,搜素材procedure TRubberDuck.Quack;
61soscw.com,搜素材begin
62soscw.com,搜素材  Writeln(‘Squeak‘);
63soscw.com,搜素材end;
64soscw.com,搜素材
65soscw.com,搜素材end.

 

3、叫声接口为鸭鸣器提供服务。叫声接口与鸭鸣器的关系比与它的实现类更密切。

 

soscw.com,搜素材
 1soscw.com,搜素材
 2soscw.com,搜素材{《HeadFirst设计模式》Delphi代码之模式小结 }
 3soscw.com,搜素材{ 鸭鸣模拟器                             }
 4soscw.com,搜素材{ 编译工具:Delphi2010 for win32         }
 5soscw.com,搜素材{ E-Mail :guzh-0417@163.com            }
 6soscw.com,搜素材
 7soscw.com,搜素材unit uDuckSimulator;
 8soscw.com,搜素材
 9soscw.com,搜素材interface
10soscw.com,搜素材
11soscw.com,搜素材uses
12soscw.com,搜素材  uQuackable, uDuck;
13soscw.com,搜素材
14soscw.com,搜素材type
15soscw.com,搜素材  TDuckSimulator = class(TObject)
16soscw.com,搜素材  private
17soscw.com,搜素材    procedure Simulate(aDuck: IQuackable); overload;
18soscw.com,搜素材  public
19soscw.com,搜素材    procedure Simulate; overload;
20soscw.com,搜素材  end;
21soscw.com,搜素材
22soscw.com,搜素材implementation
23soscw.com,搜素材
24soscw.com,搜素材{ TDuckSimulator }
25soscw.com,搜素材
26soscw.com,搜素材procedure TDuckSimulator.Simulate(aDuck: IQuackable); { 此处交给多态处理 }
27soscw.com,搜素材begin
28soscw.com,搜素材  aDuck.Quack;
29soscw.com,搜素材end;
30soscw.com,搜素材
31soscw.com,搜素材procedure TDuckSimulator.Simulate;
32soscw.com,搜素材var
33soscw.com,搜素材  aMallardDuck: IQuackable;
34soscw.com,搜素材  aRedHeadDuck: IQuackable;
35soscw.com,搜素材  aDuckCall   : IQuackable;
36soscw.com,搜素材  aRubberDuck : IQuackable;
37soscw.com,搜素材begin
38soscw.com,搜素材  try
39soscw.com,搜素材    { 创建鸭鸣实例 }
40soscw.com,搜素材    aMallardDuck := TMallardDuck.Create;
41soscw.com,搜素材    aRedHeadDuck := TRedHeadDuck.Create;
42soscw.com,搜素材    aDuckCall    := TDuckCall.Create;
43soscw.com,搜素材    aRubberDuck  := TRubberDuck.Create;
44soscw.com,搜素材
45soscw.com,搜素材    Writeln(‘Duck Simulator‘);
46soscw.com,搜素材
47soscw.com,搜素材    { 模拟鸭鸣 }
48soscw.com,搜素材    Simulate(aMallardDuck);
49soscw.com,搜素材    Simulate(aRedHeadDuck);
50soscw.com,搜素材    Simulate(aDuckCall);
51soscw.com,搜素材    Simulate(aRubberDuck );
52soscw.com,搜素材  finally
53soscw.com,搜素材    aMallardDuck := nil;
54soscw.com,搜素材    aRedHeadDuck := nil;
55soscw.com,搜素材    aDuckCall    := nil;
56soscw.com,搜素材    aRubberDuck  := nil;
57soscw.com,搜素材  end;
58soscw.com,搜素材end;
59soscw.com,搜素材
60soscw.com,搜素材end.

 

4、客户端。

 

soscw.com,搜素材
 1soscw.com,搜素材
 2soscw.com,搜素材{《HeadFirst设计模式》Delphi代码之模式小结 }
 3soscw.com,搜素材{ 客户端                                   }
 4soscw.com,搜素材{ 编译工具:Delphi2010 for win32           }
 5soscw.com,搜素材{ E-Mail :guzh-0417@163.com               }
 6soscw.com,搜素材
 7soscw.com,搜素材program pDuckSimulator;
 8soscw.com,搜素材
 9soscw.com,搜素材{$APPTYPE CONSOLE}
10soscw.com,搜素材
11soscw.com,搜素材uses
12soscw.com,搜素材  uDuck in ‘uDuck.pas‘,
13soscw.com,搜素材  uDuckSimulator in ‘uDuckSimulator.pas‘,
14soscw.com,搜素材  uQuackable in ‘uQuackable.pas‘;
15soscw.com,搜素材
16soscw.com,搜素材var
17soscw.com,搜素材  aSimulator: TDuckSimulator;
18soscw.com,搜素材
19soscw.com,搜素材begin
20soscw.com,搜素材  aSimulator := TDuckSimulator.Create;
21soscw.com,搜素材
22soscw.com,搜素材  aSimulator.Simulate;
23soscw.com,搜素材
24soscw.com,搜素材  aSimulator.Free;
25soscw.com,搜素材
26soscw.com,搜素材  Readln;
27soscw.com,搜素材end.

 

5、运行结果:

soscw.com,搜素材

 

 
 

Delphi 设计模式:《HeadFirst设计模式》Delphi代码---模式小结之一个叫声接口和几只鸭子[转]

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

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


评论


亲,登录后才可以留言!