1
2{"Headfirst design mode" decoration mode}
3{the class in this unit is the decorator}
4{compiler tool: Delphi7.0}
5{e-mail: [Email protected]}
6
7unit ucomponent;
8
9interface
10
11type
Tbeverage = Class (TObject)//abstract Beverage
Protected
fdescription:string;
Public
function getdescription:string; Virtual
+ function cost:double; Virtual Abstract
The end;
19
Tespresso = Class (Tbeverage)//Espresso beverage
Public
Constructor Create;
cost:double function; Override
The end;
25
Thouseblend = Class (Tbeverage)//specific houseblend beverage
Public
Constructor Create;
function cost:double; Override
The end;
31
Tdarkroast = Class (Tbeverage)//specific darkroast beverage
Public
Constructor Create;
function cost:double; Override
The end;
37
40implementation
41
42{Tbeverage}
43
44function TBeverage.GetDescription:String;
45begin
Result: = fdescription;
47end;
48
49{Tespresso}
50
51function TEspresso.Cost:Double;
52begin
The Result: = 1.99;
54end;
55
56constructor tespresso.create;
57begin
Fdescription: = ' Espresso ';
59end;
60
61{Thouseblend}
62
63function THouseBlend.Cost:Double;
64begin
+ Result: = 0.89;
66end;
67
68constructor thouseblend.create;
69begin
Fdescription: = ' house Blend Coffee ';
71end;
72
73{Tdarkroast}
74
75function TDarkRoast.Cost:Double;
76begin
Result: = 0.99;
78end;
79
80constructor tdarkroast.create;
81begin
Fdescription: = ' Dark roast Coffee ';
83end;
84
85end.
1
2{"Headfirst design mode" decoration mode}
3{decorators are both inherited and assembled by decorators. Inheritance
The intent of 4{here is primarily type matching--}
The 5{is the same type as the adorned person. }
6{compiler tool: Delphi7.0}
7{e-mail: [Email protected]}
8
9unit Udecorator;
10
11interface
12
13uses
Ucomponent;
15
16type
Tcondimentdecorator = Class (Tbeverage)//abstract Decorator
The end;
19
Tmocha = Class (Tcondimentdecorator)//Concrete Decorator: Mocha
$ private
Fbeverage:tbeverage;
Public
Constructor Create (abeverage:tbeverage);
function getdescription:string; Override
-function cost:double; Override
End;
28
Tsoy = Class (Tcondimentdecorator)//Concrete Decorator: Soy
Private
Fbeverage:tbeverage;
Public
Constructor Create (abeverage:tbeverage);
getdescription:string function; Override
function cost:double; Override
The end;
37
Twhip = Class (Tcondimentdecorator)//Concrete Decorator: Whip
The private
Fbeverage:tbeverage;
Public
Constructor Create (abeverage:tbeverage);
getdescription:string function; Override
cost:double function; Override
The end;
46
47implementation
48
49{Tmocha}
50
51function TMocha.Cost:Double;
52begin
Fbeverage.cost Result: = + 0.20;
54end;
55
56constructor tmocha.create (abeverage:tbeverage);
57begin
Fbeverage: = Abeverage;
59end;
60
61function TMocha.GetDescription:String;
62begin
Result: = Fbeverage.getdescription + ', Mocha ';
64end;
65
66{Tsoy}
67
68function TSoy.Cost:Double;
69begin
+ Result: = Fbeverage.cost + 0.15;
71end;
72
73constructor tsoy.create (abeverage:tbeverage);
74begin
Fbeverage: = Abeverage;
76end;
77
78function TSoy.GetDescription:String;
79begin
Result: = Fbeverage.getdescription + ', Soy ';
81end;
82
83{Twhip}
84
85function TWhip.Cost:Double;
86begin
The Result: = Fbeverage.cost + 0.10;
88end;
89
90constructor twhip.create (abeverage:tbeverage);
91begin
Fbeverage: = Abeverage;
93end;
94
95function TWhip.GetDescription:String;
96begin
The Result: = Fbeverage.getdescription + ', Whip ';
98end;
99
100end.
1
2{"Headfirst design mode" decoration mode}
3{Client}
4{compiler tool: Delphi7.0}
5{e-mail: [Email protected]}
6
7program Pstarbuzzcoffee;
8
9{$APPTYPE CONSOLE}
10
11uses
Sysutils,
Ucomponent in ' Ucomponent.pas ',
Udecorator in ' Udecorator.pas ';
15
16var
Abeverage1:tbeverage;
Abeverage2:tbeverage;
Abeverage3:tbeverage;
20
21begin
ABeverage1: = tespresso.create;
Writeln (abeverage1.getdescription + ' $ ' + floattostr (abeverage1.cost));
Freeandnil (ABEVERAGE1);
25
ABeverage2: = tdarkroast.create;
ABeverage2: = Twhip.create (Tmocha.create (Tmocha.create (ABeverage2)));
Writeln (abeverage2.getdescription + ' $ ' + floattostr (abeverage2.cost));
Freeandnil (ABeverage2);
30
ABeverage3: = thouseblend.create;
ABeverage3: = Tsoy.create (ABeverage3);
ABeverage3: = Tmocha.create (ABeverage3);
ABeverage3: = Twhip.create (ABeverage3);
Writeln (abeverage3.getdescription + ' $ ' + floattostr (abeverage3.cost));
Freeandnil (ABeverage3);
37
READLN;
39end.
Operation Result:
Delphi design mode: "Headfirst design mode" DELPHI7 Code---decorative mode starbuzzcoffee[)