Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---装饰模式之StarBuzzCoffee[转]

2020-12-13 15:39

阅读:506

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

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 uComponent;
 8soscw.com,搜素材
 9soscw.com,搜素材interface
10soscw.com,搜素材
11soscw.com,搜素材type
12soscw.com,搜素材  TBeverage = class(TObject) //抽象饮料类
13soscw.com,搜素材  protected
14soscw.com,搜素材    FDescription: String;
15soscw.com,搜素材  public
16soscw.com,搜素材    function GetDescription: String; virtual;
17soscw.com,搜素材    function Cost: Double; virtual; abstract;
18soscw.com,搜素材  end;
19soscw.com,搜素材
20soscw.com,搜素材  TEspresso = class(TBeverage) //浓咖啡饮料类
21soscw.com,搜素材  public
22soscw.com,搜素材    constructor Create;
23soscw.com,搜素材    function Cost: Double; override;
24soscw.com,搜素材  end;
25soscw.com,搜素材
26soscw.com,搜素材  THouseBlend = class(TBeverage) //具体HouseBlend饮料类
27soscw.com,搜素材  public
28soscw.com,搜素材    constructor Create;
29soscw.com,搜素材    function Cost: Double; override;
30soscw.com,搜素材  end;
31soscw.com,搜素材
32soscw.com,搜素材  TDarkRoast = class(TBeverage) //具体DarkRoast饮料类
33soscw.com,搜素材  public
34soscw.com,搜素材    constructor Create;
35soscw.com,搜素材    function Cost: Double; override;
36soscw.com,搜素材  end;
37soscw.com,搜素材
40soscw.com,搜素材implementation
41soscw.com,搜素材
42soscw.com,搜素材{ TBeverage }
43soscw.com,搜素材
44soscw.com,搜素材function TBeverage.GetDescription: String;
45soscw.com,搜素材begin
46soscw.com,搜素材  Result := FDescription;
47soscw.com,搜素材end;
48soscw.com,搜素材
49soscw.com,搜素材{ TEspresso }
50soscw.com,搜素材
51soscw.com,搜素材function TEspresso.Cost: Double;
52soscw.com,搜素材begin
53soscw.com,搜素材  Result := 1.99;
54soscw.com,搜素材end;
55soscw.com,搜素材
56soscw.com,搜素材constructor TEspresso.Create;
57soscw.com,搜素材begin
58soscw.com,搜素材  FDescription := ‘Espresso‘;
59soscw.com,搜素材end;
60soscw.com,搜素材
61soscw.com,搜素材{ THouseBlend }
62soscw.com,搜素材
63soscw.com,搜素材function THouseBlend.Cost: Double;
64soscw.com,搜素材begin
65soscw.com,搜素材  Result := 0.89;
66soscw.com,搜素材end;
67soscw.com,搜素材
68soscw.com,搜素材constructor THouseBlend.Create;
69soscw.com,搜素材begin
70soscw.com,搜素材  FDescription := ‘House Blend Coffee‘;
71soscw.com,搜素材end;
72soscw.com,搜素材
73soscw.com,搜素材{ TDarkRoast }
74soscw.com,搜素材
75soscw.com,搜素材function TDarkRoast.Cost: Double;
76soscw.com,搜素材begin
77soscw.com,搜素材  Result := 0.99;
78soscw.com,搜素材end;
79soscw.com,搜素材
80soscw.com,搜素材constructor TDarkRoast.Create;
81soscw.com,搜素材begin
82soscw.com,搜素材  FDescription := ‘Dark Roast Coffee‘;
83soscw.com,搜素材end;
84soscw.com,搜素材
85soscw.com,搜素材end.

 

soscw.com,搜素材
  1soscw.com,搜素材
  2soscw.com,搜素材{《HeadFirst设计模式》之装饰模式  }
  3soscw.com,搜素材{ 装饰者既继承又组合被装饰者。继承 }
  4soscw.com,搜素材{ 在这里的意图主要是类型匹配 ——  }
  5soscw.com,搜素材{ 与被装饰者是同一类型。          }
  6soscw.com,搜素材{ 编译工具: Delphi7.0           }
  7soscw.com,搜素材{ E-Mail : guzh-0417@163.com   }
  8soscw.com,搜素材
  9soscw.com,搜素材unit uDecorator;
 10soscw.com,搜素材
 11soscw.com,搜素材interface
 12soscw.com,搜素材
 13soscw.com,搜素材uses
 14soscw.com,搜素材  uComponent;
 15soscw.com,搜素材
 16soscw.com,搜素材type
 17soscw.com,搜素材  TCondimentDecorator = class(TBeverage) //抽象装饰者
 18soscw.com,搜素材  end;
 19soscw.com,搜素材
 20soscw.com,搜素材  TMocha = class(TCondimentDecorator) //具体装饰者:Mocha
 21soscw.com,搜素材  private
 22soscw.com,搜素材    FBeverage: TBeverage;
 23soscw.com,搜素材  public
 24soscw.com,搜素材    constructor Create(aBeverage: TBeverage);
 25soscw.com,搜素材    function GetDescription: String; override;
 26soscw.com,搜素材    function Cost: Double; override;
 27soscw.com,搜素材  end;
 28soscw.com,搜素材
 29soscw.com,搜素材  TSoy = class(TCondimentDecorator) //具体装饰者:Soy
 30soscw.com,搜素材  private
 31soscw.com,搜素材    FBeverage: TBeverage;
 32soscw.com,搜素材  public
 33soscw.com,搜素材    constructor Create(aBeverage: TBeverage);
 34soscw.com,搜素材    function GetDescription: String; override;
 35soscw.com,搜素材    function Cost: Double; override;
 36soscw.com,搜素材  end;
 37soscw.com,搜素材
 38soscw.com,搜素材  TWhip = class(TCondimentDecorator) //具体装饰者:Whip
 39soscw.com,搜素材  private
 40soscw.com,搜素材    FBeverage: TBeverage;
 41soscw.com,搜素材  public
 42soscw.com,搜素材    constructor Create(aBeverage: TBeverage);
 43soscw.com,搜素材    function GetDescription: String; override;
 44soscw.com,搜素材    function Cost: Double; override;
 45soscw.com,搜素材  end;
 46soscw.com,搜素材
 47soscw.com,搜素材implementation
 48soscw.com,搜素材
 49soscw.com,搜素材{ TMocha }
 50soscw.com,搜素材
 51soscw.com,搜素材function TMocha.Cost: Double;
 52soscw.com,搜素材begin
 53soscw.com,搜素材  Result := FBeverage.Cost + 0.20;
 54soscw.com,搜素材end;
 55soscw.com,搜素材
 56soscw.com,搜素材constructor TMocha.Create(aBeverage: TBeverage);
 57soscw.com,搜素材begin
 58soscw.com,搜素材  FBeverage := aBeverage;
 59soscw.com,搜素材end;
 60soscw.com,搜素材
 61soscw.com,搜素材function TMocha.GetDescription: String;
 62soscw.com,搜素材begin
 63soscw.com,搜素材  Result := FBeverage.GetDescription + ‘, Mocha‘;
 64soscw.com,搜素材end;
 65soscw.com,搜素材
 66soscw.com,搜素材{ TSoy }
 67soscw.com,搜素材
 68soscw.com,搜素材function TSoy.Cost: Double;
 69soscw.com,搜素材begin
 70soscw.com,搜素材  Result := FBeverage.Cost + 0.15;
 71soscw.com,搜素材end;
 72soscw.com,搜素材
 73soscw.com,搜素材constructor TSoy.Create(aBeverage: TBeverage);
 74soscw.com,搜素材begin
 75soscw.com,搜素材  FBeverage := aBeverage;
 76soscw.com,搜素材end;
 77soscw.com,搜素材
 78soscw.com,搜素材function TSoy.GetDescription: String;
 79soscw.com,搜素材begin
 80soscw.com,搜素材  Result := FBeverage.GetDescription + ‘, Soy‘;
 81soscw.com,搜素材end;
 82soscw.com,搜素材
 83soscw.com,搜素材{ TWhip }
 84soscw.com,搜素材
 85soscw.com,搜素材function TWhip.Cost: Double;
 86soscw.com,搜素材begin
 87soscw.com,搜素材  Result := FBeverage.Cost + 0.10;
 88soscw.com,搜素材end;
 89soscw.com,搜素材
 90soscw.com,搜素材constructor TWhip.Create(aBeverage: TBeverage);
 91soscw.com,搜素材begin
 92soscw.com,搜素材  FBeverage := aBeverage;
 93soscw.com,搜素材end;
 94soscw.com,搜素材
 95soscw.com,搜素材function TWhip.GetDescription: String;
 96soscw.com,搜素材begin
 97soscw.com,搜素材  Result := FBeverage.GetDescription + ‘, Whip‘;
 98soscw.com,搜素材end;
 99soscw.com,搜素材
100soscw.com,搜素材end.

 

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 pStarBuzzCoffee;
 8soscw.com,搜素材
 9soscw.com,搜素材{$APPTYPE CONSOLE}
10soscw.com,搜素材
11soscw.com,搜素材uses
12soscw.com,搜素材  SysUtils,
13soscw.com,搜素材  uComponent in ‘uComponent.pas‘,
14soscw.com,搜素材  uDecorator in ‘uDecorator.pas‘;
15soscw.com,搜素材
16soscw.com,搜素材var
17soscw.com,搜素材  aBeverage1: TBeverage;
18soscw.com,搜素材  aBeverage2: TBeverage;
19soscw.com,搜素材  aBeverage3: TBeverage;
20soscw.com,搜素材
21soscw.com,搜素材begin
22soscw.com,搜素材  aBeverage1 := TEspresso.Create;
23soscw.com,搜素材  Writeln(aBeverage1.GetDescription + ‘ $‘ + FloatToStr(aBeverage1.Cost));
24soscw.com,搜素材  FreeAndNil(aBeverage1);
25soscw.com,搜素材
26soscw.com,搜素材  aBeverage2 := TDarkRoast.Create;
27soscw.com,搜素材  aBeverage2 := TWhip.Create(TMocha.Create(TMocha.Create(aBeverage2)));
28soscw.com,搜素材  Writeln(aBeverage2.GetDescription + ‘ $‘ + FloatToStr(aBeverage2.Cost));
29soscw.com,搜素材  FreeAndNil(aBeverage2);
30soscw.com,搜素材
31soscw.com,搜素材  aBeverage3 := THouseBlend.Create;
32soscw.com,搜素材  aBeverage3 := TSoy.Create(aBeverage3);
33soscw.com,搜素材  aBeverage3 := TMocha.Create(aBeverage3);
34soscw.com,搜素材  aBeverage3 := TWhip.Create(aBeverage3);
35soscw.com,搜素材  Writeln(aBeverage3.GetDescription + ‘ $‘ + FloatToStr(aBeverage3.Cost));
36soscw.com,搜素材  FreeAndNil(aBeverage3);
37soscw.com,搜素材  
38soscw.com,搜素材  Readln;
39soscw.com,搜素材end.

 

运行结果:

soscw.com,搜素材

 

 
 

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---装饰模式之StarBuzzCoffee[转]

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

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


评论


亲,登录后才可以留言!