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

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

时间:2014-11-05 16:20:13      阅读:311      评论:0      收藏:0      [点我收藏+]

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

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

 

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

 

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

 

运行结果:

bubuko.com,布布扣

 

 
 

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

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

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

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