首页 > 代码库 > Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---装饰模式之StarBuzzCoffee[转]
Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---装饰模式之StarBuzzCoffee[转]
1
2{《HeadFirst设计模式》之装饰模式 }
3{ 本单元中的类为被装饰者 }
4{ 编译工具: Delphi7.0 }
5{ E-Mail : guzh-0417@163.com }
6
7unit uComponent;
8
9interface
10
11type
12 TBeverage = class(TObject) //抽象饮料类
13 protected
14 FDescription: String;
15 public
16 function GetDescription: String; virtual;
17 function Cost: Double; virtual; abstract;
18 end;
19
20 TEspresso = class(TBeverage) //浓咖啡饮料类
21 public
22 constructor Create;
23 function Cost: Double; override;
24 end;
25
26 THouseBlend = class(TBeverage) //具体HouseBlend饮料类
27 public
28 constructor Create;
29 function Cost: Double; override;
30 end;
31
32 TDarkRoast = class(TBeverage) //具体DarkRoast饮料类
33 public
34 constructor Create;
35 function Cost: Double; override;
36 end;
37
40implementation
41
42{ TBeverage }
43
44function TBeverage.GetDescription: String;
45begin
46 Result := FDescription;
47end;
48
49{ TEspresso }
50
51function TEspresso.Cost: Double;
52begin
53 Result := 1.99;
54end;
55
56constructor TEspresso.Create;
57begin
58 FDescription := ‘Espresso‘;
59end;
60
61{ THouseBlend }
62
63function THouseBlend.Cost: Double;
64begin
65 Result := 0.89;
66end;
67
68constructor THouseBlend.Create;
69begin
70 FDescription := ‘House Blend Coffee‘;
71end;
72
73{ TDarkRoast }
74
75function TDarkRoast.Cost: Double;
76begin
77 Result := 0.99;
78end;
79
80constructor TDarkRoast.Create;
81begin
82 FDescription := ‘Dark Roast Coffee‘;
83end;
84
85end.
1
2{《HeadFirst设计模式》之装饰模式 }
3{ 装饰者既继承又组合被装饰者。继承 }
4{ 在这里的意图主要是类型匹配 —— }
5{ 与被装饰者是同一类型。 }
6{ 编译工具: Delphi7.0 }
7{ E-Mail : guzh-0417@163.com }
8
9unit uDecorator;
10
11interface
12
13uses
14 uComponent;
15
16type
17 TCondimentDecorator = class(TBeverage) //抽象装饰者
18 end;
19
20 TMocha = class(TCondimentDecorator) //具体装饰者:Mocha
21 private
22 FBeverage: TBeverage;
23 public
24 constructor Create(aBeverage: TBeverage);
25 function GetDescription: String; override;
26 function Cost: Double; override;
27 end;
28
29 TSoy = class(TCondimentDecorator) //具体装饰者:Soy
30 private
31 FBeverage: TBeverage;
32 public
33 constructor Create(aBeverage: TBeverage);
34 function GetDescription: String; override;
35 function Cost: Double; override;
36 end;
37
38 TWhip = class(TCondimentDecorator) //具体装饰者:Whip
39 private
40 FBeverage: TBeverage;
41 public
42 constructor Create(aBeverage: TBeverage);
43 function GetDescription: String; override;
44 function Cost: Double; override;
45 end;
46
47implementation
48
49{ TMocha }
50
51function TMocha.Cost: Double;
52begin
53 Result := FBeverage.Cost + 0.20;
54end;
55
56constructor TMocha.Create(aBeverage: TBeverage);
57begin
58 FBeverage := aBeverage;
59end;
60
61function TMocha.GetDescription: String;
62begin
63 Result := FBeverage.GetDescription + ‘, Mocha‘;
64end;
65
66{ TSoy }
67
68function TSoy.Cost: Double;
69begin
70 Result := FBeverage.Cost + 0.15;
71end;
72
73constructor TSoy.Create(aBeverage: TBeverage);
74begin
75 FBeverage := aBeverage;
76end;
77
78function TSoy.GetDescription: String;
79begin
80 Result := FBeverage.GetDescription + ‘, Soy‘;
81end;
82
83{ TWhip }
84
85function TWhip.Cost: Double;
86begin
87 Result := FBeverage.Cost + 0.10;
88end;
89
90constructor TWhip.Create(aBeverage: TBeverage);
91begin
92 FBeverage := aBeverage;
93end;
94
95function TWhip.GetDescription: String;
96begin
97 Result := FBeverage.GetDescription + ‘, Whip‘;
98end;
99
100end.
1
2{《HeadFirst设计模式》之装饰模式 }
3{ 客户端 }
4{ 编译工具: Delphi7.0 }
5{ E-Mail : guzh-0417@163.com }
6
7program pStarBuzzCoffee;
8
9{$APPTYPE CONSOLE}
10
11uses
12 SysUtils,
13 uComponent in ‘uComponent.pas‘,
14 uDecorator in ‘uDecorator.pas‘;
15
16var
17 aBeverage1: TBeverage;
18 aBeverage2: TBeverage;
19 aBeverage3: TBeverage;
20
21begin
22 aBeverage1 := TEspresso.Create;
23 Writeln(aBeverage1.GetDescription + ‘ $‘ + FloatToStr(aBeverage1.Cost));
24 FreeAndNil(aBeverage1);
25
26 aBeverage2 := TDarkRoast.Create;
27 aBeverage2 := TWhip.Create(TMocha.Create(TMocha.Create(aBeverage2)));
28 Writeln(aBeverage2.GetDescription + ‘ $‘ + FloatToStr(aBeverage2.Cost));
29 FreeAndNil(aBeverage2);
30
31 aBeverage3 := THouseBlend.Create;
32 aBeverage3 := TSoy.Create(aBeverage3);
33 aBeverage3 := TMocha.Create(aBeverage3);
34 aBeverage3 := TWhip.Create(aBeverage3);
35 Writeln(aBeverage3.GetDescription + ‘ $‘ + FloatToStr(aBeverage3.Cost));
36 FreeAndNil(aBeverage3);
37
38 Readln;
39end.
运行结果:
Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---装饰模式之StarBuzzCoffee[转]
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。