码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2014-11-05 16:53:59      阅读:246      评论:0      收藏:0      [点我收藏+]

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

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

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

 

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

 

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

 

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

 

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

 

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

 

4、客户端。

 

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

 

5、运行结果:

bubuko.com,布布扣

 

 
 

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!