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

2020-12-13 15:44

阅读:533

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

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 uStrategy;
 8soscw.com,搜素材
 9soscw.com,搜素材interface
10soscw.com,搜素材
11soscw.com,搜素材type
12soscw.com,搜素材  {飞行接口,及其实现类 }
13soscw.com,搜素材
14soscw.com,搜素材  IFlyBehavior = Interface(IInterface)
15soscw.com,搜素材    procedure Fly;
16soscw.com,搜素材  end;
17soscw.com,搜素材
18soscw.com,搜素材  TFlyWithWings = class(TInterfacedObject, IFlyBehavior)
19soscw.com,搜素材  public
20soscw.com,搜素材    procedure Fly;
21soscw.com,搜素材  end;
22soscw.com,搜素材
23soscw.com,搜素材  TFlyNoWay = class(TInterfacedObject, IFlyBehavior)
24soscw.com,搜素材  public
25soscw.com,搜素材    procedure Fly;
26soscw.com,搜素材  end;
27soscw.com,搜素材
28soscw.com,搜素材  TFlyRocketPowered = class(TInterfacedObject, IFlyBehavior)
29soscw.com,搜素材  public
30soscw.com,搜素材    procedure Fly;
31soscw.com,搜素材  end;
32soscw.com,搜素材
33soscw.com,搜素材  {叫声接口,及其实现类}
34soscw.com,搜素材
35soscw.com,搜素材  IQuackBehavior = Interface(IInterface)
36soscw.com,搜素材    procedure Quack;
37soscw.com,搜素材  end;
38soscw.com,搜素材
39soscw.com,搜素材  TQuack = class(TInterfacedObject, IQuackBehavior)
40soscw.com,搜素材  public
41soscw.com,搜素材    procedure Quack;
42soscw.com,搜素材  end;
43soscw.com,搜素材
44soscw.com,搜素材  TMuteQuack = class(TInterfacedObject, IQuackBehavior)
45soscw.com,搜素材  public
46soscw.com,搜素材    procedure Quack;
47soscw.com,搜素材  end;
48soscw.com,搜素材
49soscw.com,搜素材  TSqueak = class(TInterfacedObject, IQuackBehavior)
50soscw.com,搜素材  public
51soscw.com,搜素材    procedure Quack;
52soscw.com,搜素材  end;
53soscw.com,搜素材
54soscw.com,搜素材implementation
55soscw.com,搜素材
56soscw.com,搜素材{ TFlyWithWings }
57soscw.com,搜素材
58soscw.com,搜素材procedure TFlyWithWings.Fly;
59soscw.com,搜素材begin
60soscw.com,搜素材  Writeln(‘I am flying!‘);
61soscw.com,搜素材end;
62soscw.com,搜素材
63soscw.com,搜素材{ TFlyNoWay }
64soscw.com,搜素材
65soscw.com,搜素材procedure TFlyNoWay.Fly;
66soscw.com,搜素材begin
67soscw.com,搜素材  Writeln(‘I can not fly!‘);
68soscw.com,搜素材end;
69soscw.com,搜素材
70soscw.com,搜素材{ TFlyRocketPowered }
71soscw.com,搜素材
72soscw.com,搜素材procedure TFlyRocketPowered.Fly;
73soscw.com,搜素材begin
74soscw.com,搜素材  Writeln(‘I am flying with a rocket!‘);
75soscw.com,搜素材end;
76soscw.com,搜素材
77soscw.com,搜素材{ TQuack }
78soscw.com,搜素材
79soscw.com,搜素材procedure TQuack.Quack;
80soscw.com,搜素材begin
81soscw.com,搜素材  Writeln(‘Quack‘);
82soscw.com,搜素材end;
83soscw.com,搜素材
84soscw.com,搜素材{ TMuteQuack }
85soscw.com,搜素材
86soscw.com,搜素材procedure TMuteQuack.Quack;
87soscw.com,搜素材begin
88soscw.com,搜素材  Writeln(‘‘);
89soscw.com,搜素材end;
90soscw.com,搜素材
91soscw.com,搜素材{ TSqueak }
92soscw.com,搜素材
93soscw.com,搜素材procedure TSqueak.Quack;
94soscw.com,搜素材begin
95soscw.com,搜素材  Writeln(‘Squeak‘);
96soscw.com,搜素材end;
97soscw.com,搜素材
98soscw.com,搜素材end.

 

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

 

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 pMiniDuckSimulator;
 8soscw.com,搜素材
 9soscw.com,搜素材{$APPTYPE CONSOLE}
10soscw.com,搜素材
11soscw.com,搜素材uses
12soscw.com,搜素材  SysUtils,
13soscw.com,搜素材  uDuck in ‘uDuck.pas‘,
14soscw.com,搜素材  uStrategy in ‘uStrategy.pas‘;
15soscw.com,搜素材
16soscw.com,搜素材var
17soscw.com,搜素材  Mallard: TDuck;
18soscw.com,搜素材  Model  : TDuck;
19soscw.com,搜素材  FlyRocketPowered: IFlyBehavior;
20soscw.com,搜素材  
21soscw.com,搜素材begin
22soscw.com,搜素材  Mallard := TMallardDuck.Create;
23soscw.com,搜素材  Mallard.PerformQuack;
24soscw.com,搜素材  Mallard.PerformFly;
25soscw.com,搜素材  FreeAndNil(Mallard);
26soscw.com,搜素材
27soscw.com,搜素材  Model := TModelDuck.Create;
28soscw.com,搜素材  Model.PerformFly;
29soscw.com,搜素材
30soscw.com,搜素材  FlyRocketPowered := TFlyRocketPowered.Create;
31soscw.com,搜素材  Model.SetFlyBehavior(FlyRocketPowered);
32soscw.com,搜素材  Model.PerformFly;
33soscw.com,搜素材  FlyRocketPowered := nil;
34soscw.com,搜素材  FreeAndNil(Model);
35soscw.com,搜素材
36soscw.com,搜素材  Readln;
37soscw.com,搜素材end.

 

运行结果:

soscw.com,搜素材

 

 
 

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

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

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


评论


亲,登录后才可以留言!