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

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---策略模式之MiniDuckSimulator[转]

时间:2014-11-05 16:39:35      阅读:330      评论:0      收藏:0      [点我收藏+]

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

bubuko.com,布布扣
 1bubuko.com,布布扣
 2bubuko.com,布布扣{《HeadFirst设计模式》之策略模式 }
 3bubuko.com,布布扣{ 本单元中的类为策略类           }
 4bubuko.com,布布扣{ 编译工具: Delphi7.0           }
 5bubuko.com,布布扣{ E-Mail : guzh-0417@163.com   }
 6bubuko.com,布布扣
 7bubuko.com,布布扣unit uStrategy;
 8bubuko.com,布布扣
 9bubuko.com,布布扣interface
10bubuko.com,布布扣
11bubuko.com,布布扣type
12bubuko.com,布布扣  {飞行接口,及其实现类 }
13bubuko.com,布布扣
14bubuko.com,布布扣  IFlyBehavior = Interface(IInterface)
15bubuko.com,布布扣    procedure Fly;
16bubuko.com,布布扣  end;
17bubuko.com,布布扣
18bubuko.com,布布扣  TFlyWithWings = class(TInterfacedObject, IFlyBehavior)
19bubuko.com,布布扣  public
20bubuko.com,布布扣    procedure Fly;
21bubuko.com,布布扣  end;
22bubuko.com,布布扣
23bubuko.com,布布扣  TFlyNoWay = class(TInterfacedObject, IFlyBehavior)
24bubuko.com,布布扣  public
25bubuko.com,布布扣    procedure Fly;
26bubuko.com,布布扣  end;
27bubuko.com,布布扣
28bubuko.com,布布扣  TFlyRocketPowered = class(TInterfacedObject, IFlyBehavior)
29bubuko.com,布布扣  public
30bubuko.com,布布扣    procedure Fly;
31bubuko.com,布布扣  end;
32bubuko.com,布布扣
33bubuko.com,布布扣  {叫声接口,及其实现类}
34bubuko.com,布布扣
35bubuko.com,布布扣  IQuackBehavior = Interface(IInterface)
36bubuko.com,布布扣    procedure Quack;
37bubuko.com,布布扣  end;
38bubuko.com,布布扣
39bubuko.com,布布扣  TQuack = class(TInterfacedObject, IQuackBehavior)
40bubuko.com,布布扣  public
41bubuko.com,布布扣    procedure Quack;
42bubuko.com,布布扣  end;
43bubuko.com,布布扣
44bubuko.com,布布扣  TMuteQuack = class(TInterfacedObject, IQuackBehavior)
45bubuko.com,布布扣  public
46bubuko.com,布布扣    procedure Quack;
47bubuko.com,布布扣  end;
48bubuko.com,布布扣
49bubuko.com,布布扣  TSqueak = class(TInterfacedObject, IQuackBehavior)
50bubuko.com,布布扣  public
51bubuko.com,布布扣    procedure Quack;
52bubuko.com,布布扣  end;
53bubuko.com,布布扣
54bubuko.com,布布扣implementation
55bubuko.com,布布扣
56bubuko.com,布布扣{ TFlyWithWings }
57bubuko.com,布布扣
58bubuko.com,布布扣procedure TFlyWithWings.Fly;
59bubuko.com,布布扣begin
60bubuko.com,布布扣  Writeln(‘I am flying!‘);
61bubuko.com,布布扣end;
62bubuko.com,布布扣
63bubuko.com,布布扣{ TFlyNoWay }
64bubuko.com,布布扣
65bubuko.com,布布扣procedure TFlyNoWay.Fly;
66bubuko.com,布布扣begin
67bubuko.com,布布扣  Writeln(‘I can not fly!‘);
68bubuko.com,布布扣end;
69bubuko.com,布布扣
70bubuko.com,布布扣{ TFlyRocketPowered }
71bubuko.com,布布扣
72bubuko.com,布布扣procedure TFlyRocketPowered.Fly;
73bubuko.com,布布扣begin
74bubuko.com,布布扣  Writeln(‘I am flying with a rocket!‘);
75bubuko.com,布布扣end;
76bubuko.com,布布扣
77bubuko.com,布布扣{ TQuack }
78bubuko.com,布布扣
79bubuko.com,布布扣procedure TQuack.Quack;
80bubuko.com,布布扣begin
81bubuko.com,布布扣  Writeln(‘Quack‘);
82bubuko.com,布布扣end;
83bubuko.com,布布扣
84bubuko.com,布布扣{ TMuteQuack }
85bubuko.com,布布扣
86bubuko.com,布布扣procedure TMuteQuack.Quack;
87bubuko.com,布布扣begin
88bubuko.com,布布扣  Writeln(‘<Silence>‘);
89bubuko.com,布布扣end;
90bubuko.com,布布扣
91bubuko.com,布布扣{ TSqueak }
92bubuko.com,布布扣
93bubuko.com,布布扣procedure TSqueak.Quack;
94bubuko.com,布布扣begin
95bubuko.com,布布扣  Writeln(‘Squeak‘);
96bubuko.com,布布扣end;
97bubuko.com,布布扣
98bubuko.com,布布扣end.

 

bubuko.com,布布扣
  1bubuko.com,布布扣
  2bubuko.com,布布扣{《HeadFirst设计模式》之策略模式           }
  3bubuko.com,布布扣{ 本单元中的类为策略的用户,                }
  4bubuko.com,布布扣{ 一般策略模式中的上下文接口已包含在用户类中。}
  5bubuko.com,布布扣{ 编译工具: Delphi7.0                     }
  6bubuko.com,布布扣{ E-Mail : guzh-0417@163.com             }
  7bubuko.com,布布扣
  8bubuko.com,布布扣unit uDuck;
  9bubuko.com,布布扣
 10bubuko.com,布布扣interface
 11bubuko.com,布布扣
 12bubuko.com,布布扣uses
 13bubuko.com,布布扣  uStrategy;
 14bubuko.com,布布扣
 15bubuko.com,布布扣type
 16bubuko.com,布布扣  { 鸭子抽象类 }
 17bubuko.com,布布扣
 18bubuko.com,布布扣  TDuck = class(TObject)
 19bubuko.com,布布扣  protected
 20bubuko.com,布布扣    FFlyBehavior  : IFlyBehavior;
 21bubuko.com,布布扣    FQuackBehavior: IQuackBehavior;
 22bubuko.com,布布扣  public
 23bubuko.com,布布扣    procedure Display; virtual; abstract;
 24bubuko.com,布布扣    procedure PerformFly;
 25bubuko.com,布布扣    procedure PerformQuack;
 26bubuko.com,布布扣    procedure Swim;
 27bubuko.com,布布扣    procedure SetFlyBehavior(FlyBehavior: IFlyBehavior);
 28bubuko.com,布布扣    procedure SetQuackBehavior(QuackBehavior: IQuackBehavior);
 29bubuko.com,布布扣  end;
 30bubuko.com,布布扣
 31bubuko.com,布布扣  { 绿头鸭具体类 }
 32bubuko.com,布布扣
 33bubuko.com,布布扣  TMallardDuck = class(TDuck)
 34bubuko.com,布布扣  public
 35bubuko.com,布布扣    constructor Create;
 36bubuko.com,布布扣    destructor Destroy; override;
 37bubuko.com,布布扣    procedure Display; override;
 38bubuko.com,布布扣  end;
 39bubuko.com,布布扣
 40bubuko.com,布布扣  { 模型鸭具体类 }
 41bubuko.com,布布扣  
 42bubuko.com,布布扣  TModelDuck = class(TDuck)
 43bubuko.com,布布扣  public
 44bubuko.com,布布扣    constructor Create;
 45bubuko.com,布布扣    destructor Destroy; override;
 46bubuko.com,布布扣    procedure Display; override;
 47bubuko.com,布布扣  end;
 48bubuko.com,布布扣
 49bubuko.com,布布扣implementation
 50bubuko.com,布布扣
 51bubuko.com,布布扣{ TDuck }
 52bubuko.com,布布扣
 53bubuko.com,布布扣procedure TDuck.PerformFly;
 54bubuko.com,布布扣begin
 55bubuko.com,布布扣  FFlyBehavior.Fly;
 56bubuko.com,布布扣end;
 57bubuko.com,布布扣
 58bubuko.com,布布扣procedure TDuck.PerformQuack;
 59bubuko.com,布布扣begin
 60bubuko.com,布布扣  FQuackBehavior.Quack;
 61bubuko.com,布布扣end;
 62bubuko.com,布布扣
 63bubuko.com,布布扣procedure TDuck.SetFlyBehavior(FlyBehavior: IFlyBehavior);
 64bubuko.com,布布扣begin
 65bubuko.com,布布扣  FFlyBehavior := FlyBehavior;
 66bubuko.com,布布扣end;
 67bubuko.com,布布扣
 68bubuko.com,布布扣procedure TDuck.SetQuackBehavior(QuackBehavior: IQuackBehavior);
 69bubuko.com,布布扣begin
 70bubuko.com,布布扣  FQuackBehavior := QuackBehavior;
 71bubuko.com,布布扣end;
 72bubuko.com,布布扣
 73bubuko.com,布布扣procedure TDuck.Swim;
 74bubuko.com,布布扣begin
 75bubuko.com,布布扣  Writeln(‘All ducks float, even decoys!‘);
 76bubuko.com,布布扣end;
 77bubuko.com,布布扣
 78bubuko.com,布布扣{ TMallardDuck }
 79bubuko.com,布布扣
 80bubuko.com,布布扣constructor TMallardDuck.Create;
 81bubuko.com,布布扣begin
 82bubuko.com,布布扣  FQuackBehavior := TQuack.Create;
 83bubuko.com,布布扣  FFlyBehavior   := TFlyWithWings.Create;
 84bubuko.com,布布扣end;
 85bubuko.com,布布扣
 86bubuko.com,布布扣destructor TMallardDuck.Destroy;
 87bubuko.com,布布扣begin
 88bubuko.com,布布扣  FQuackBehavior := nil;
 89bubuko.com,布布扣  FFlyBehavior   := nil;
 90bubuko.com,布布扣  inherited;
 91bubuko.com,布布扣end;
 92bubuko.com,布布扣
 93bubuko.com,布布扣procedure TMallardDuck.Display;
 94bubuko.com,布布扣begin
 95bubuko.com,布布扣  Writeln(‘I am a real mallard duck‘);
 96bubuko.com,布布扣end;
 97bubuko.com,布布扣
 98bubuko.com,布布扣{ TModelDuck }
 99bubuko.com,布布扣
100bubuko.com,布布扣constructor TModelDuck.Create;
101bubuko.com,布布扣begin
102bubuko.com,布布扣  FFlyBehavior   := TFlyNoWay.Create;
103bubuko.com,布布扣  FQuackBehavior := TQuack.Create;
104bubuko.com,布布扣end;
105bubuko.com,布布扣
106bubuko.com,布布扣destructor TModelDuck.Destroy;
107bubuko.com,布布扣begin
108bubuko.com,布布扣  FQuackBehavior := nil;
109bubuko.com,布布扣  FFlyBehavior   := nil;
110bubuko.com,布布扣  inherited;
111bubuko.com,布布扣end;
112bubuko.com,布布扣
113bubuko.com,布布扣procedure TModelDuck.Display;
114bubuko.com,布布扣begin
115bubuko.com,布布扣  Writeln(‘I am a model duck‘);
116bubuko.com,布布扣end;
117bubuko.com,布布扣
118bubuko.com,布布扣end.
119bubuko.com,布布扣 

 

bubuko.com,布布扣
 1bubuko.com,布布扣
 2bubuko.com,布布扣{《HeadFirst设计模式》之策略模式 }
 3bubuko.com,布布扣{ 客户端                       }
 4bubuko.com,布布扣{ 编译工具: Delphi7.0          }
 5bubuko.com,布布扣{ E-Mail : guzh-0417@163.com  }
 6bubuko.com,布布扣
 7bubuko.com,布布扣program pMiniDuckSimulator;
 8bubuko.com,布布扣
 9bubuko.com,布布扣{$APPTYPE CONSOLE}
10bubuko.com,布布扣
11bubuko.com,布布扣uses
12bubuko.com,布布扣  SysUtils,
13bubuko.com,布布扣  uDuck in ‘uDuck.pas‘,
14bubuko.com,布布扣  uStrategy in ‘uStrategy.pas‘;
15bubuko.com,布布扣
16bubuko.com,布布扣var
17bubuko.com,布布扣  Mallard: TDuck;
18bubuko.com,布布扣  Model  : TDuck;
19bubuko.com,布布扣  FlyRocketPowered: IFlyBehavior;
20bubuko.com,布布扣  
21bubuko.com,布布扣begin
22bubuko.com,布布扣  Mallard := TMallardDuck.Create;
23bubuko.com,布布扣  Mallard.PerformQuack;
24bubuko.com,布布扣  Mallard.PerformFly;
25bubuko.com,布布扣  FreeAndNil(Mallard);
26bubuko.com,布布扣
27bubuko.com,布布扣  Model := TModelDuck.Create;
28bubuko.com,布布扣  Model.PerformFly;
29bubuko.com,布布扣
30bubuko.com,布布扣  FlyRocketPowered := TFlyRocketPowered.Create;
31bubuko.com,布布扣  Model.SetFlyBehavior(FlyRocketPowered);
32bubuko.com,布布扣  Model.PerformFly;
33bubuko.com,布布扣  FlyRocketPowered := nil;
34bubuko.com,布布扣  FreeAndNil(Model);
35bubuko.com,布布扣
36bubuko.com,布布扣  Readln;
37bubuko.com,布布扣end.

 

运行结果:

bubuko.com,布布扣

 

 
 

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---策略模式之MiniDuckSimulator[转]

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

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

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