Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---组合模式之Menus[转]
2020-12-13 15:40
标签:des blog http io ar os for sp div
Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---组合模式之Menus[转] 标签:des blog http io ar os for sp div 原文地址:http://www.cnblogs.com/0x2D-0x22/p/4076377.html![]()
1
2
{《HeadFirst设计模式》之组合模式 }
3
{ 组合与单项的抽象父类 }
4
{ 编译工具:Delphi2007 for win32}
5
{ E-Mail :guzh-0417@163.com }
6
7
unit uMenuComponent;
8
9
interface
10
11
uses
12
SysUtils;
13
14
type
15
TMenuComponent = class abstract(TObject)
16
public
17
procedure Add(aMenuComponent: TMenuComponent); virtual;
18
procedure Remove(aMenuComponent: TMenuComponent); virtual;
19
function GetChild(i: Integer): TMenuComponent; virtual;
20
function GetName: string; virtual;
21
function GetDescription: string; virtual;
22
function GetPrice: Integer; virtual;
23
function IsVegetarian: Boolean; virtual;
24
procedure Print; virtual;
25
end;
26
27
implementation
28
29
{ TMenuComponent }
30
31
procedure TMenuComponent.Add(aMenuComponent: TMenuComponent);
32
begin
33
raise Exception.Create(‘UnSupported Operation Exception!‘);
34
end;
35
36
function TMenuComponent.GetChild(i: Integer): TMenuComponent;
37
begin
38
raise Exception.Create(‘UnSupported Operation Exception!‘);
39
end;
40
41
function TMenuComponent.GetDescription: string;
42
begin
43
raise Exception.Create(‘UnSupported Operation Exception!‘);
44
end;
45
46
function TMenuComponent.GetName: string;
47
begin
48
raise Exception.Create(‘UnSupported Operation Exception!‘);
49
end;
50
51
function TMenuComponent.GetPrice: Integer;
52
begin
53
raise Exception.Create(‘UnSupported Operation Exception!‘);
54
end;
55
56
function TMenuComponent.IsVegetarian: Boolean;
57
begin
58
raise Exception.Create(‘UnSupported Operation Exception!‘);
59
end;
60
61
procedure TMenuComponent.Print;
62
begin
63
raise Exception.Create(‘UnSupported Operation Exception!‘);
64
end;
65
66
procedure TMenuComponent.Remove(aMenuComponent: TMenuComponent);
67
begin
68
raise Exception.Create(‘UnSupported Operation Exception!‘);
69
end;
70
71
end.
![]()
1
2
{《HeadFirst设计模式》之组合模式 }
3
{ 单项类 }
4
{ 编译工具:Delphi2007 for win32 }
5
{ E-Mail :guzh-0417@163.com }
6
7
unit uMenuItem;
8
9
interface
10
11
uses
12
uMenuComponent;
13
14
type
15
TMenuItem = class(TMenuComponent)
16
private
17
FName : string;
18
FDescription: string;
19
FVegetarian : Boolean;
20
FPrice: Integer;
21
public
22
constructor Create(aName, aDescription: string;
23
aVegetarian : Boolean;
24
aPrice: Integer);
25
function GetName: string; override;
26
function GetDescription: string; override;
27
function GetPrice: Integer; override;
28
function IsVegetarian: Boolean; override;
29
procedure Print; override;
30
end;
31
32
implementation
33
34
{ TMenuItem }
35
36
constructor TMenuItem.Create(aName, aDescription: string;
37
aVegetarian: Boolean;
38
aPrice: Integer);
39
begin
40
FName := aName;
41
FDescription := aDescription;
42
FVegetarian := aVegetarian;
43
FPrice := aPrice;
44
end;
45
46
function TMenuItem.GetDescription: string;
47
begin
48
Result := FDescription
49
end;
50
51
function TMenuItem.GetName: string;
52
begin
53
Result := FName;
54
end;
55
56
function TMenuItem.GetPrice: Integer;
57
begin
58
Result := FPrice;
59
end;
60
61
function TMenuItem.IsVegetarian: Boolean;
62
begin
63
Result := FVegetarian
64
end;
65
66
procedure TMenuItem.Print;
67
begin
68
Write(‘ ‘ + GetName);
69
if IsVegetarian then
70
begin
71
Write(‘(V)‘);
72
end;
73
Writeln(‘, ‘, GetPrice);
74
Writeln(‘ --‘ + GetDescription);
75
end;
76
77
end.
![]()
1
2
{《HeadFirst设计模式》之组合模式 }
3
{ 组合类 }
4
{ 编译工具:Delphi2007 for win32 }
5
{ E-Mail :guzh-0417@163.com }
6
7
unit uMenu;
8
9
interface
10
11
uses
12
uMenuComponent, Classes;
13
14
type
15
TMenu = class(TMenuComponent)
16
private
17
FMenuComponents: TList;
18
FName: string;
19
FDescription: string;
20
public
21
constructor Create(aName, aDescription: string);
22
destructor Destroy; override;
23
procedure Add(aMenuComponent: TMenuComponent); override;
24
procedure Remove(aMenuComponent: TMenuComponent); override;
25
function GetChild(i: Integer): TMenuComponent; override;
26
function GetName: string; override;
27
function GetDescription: string; override;
28
procedure Print; override;
29
end;
30
31
implementation
32
33
{ TMenu }
34
35
constructor TMenu.Create(aName, aDescription: string);
36
begin
37
FMenuComponents := TList.Create;
38
FName := aName;
39
FDescription := aDescription;
40
end;
41
42
destructor TMenu.Destroy;
43
begin
44
FMenuComponents.Clear;
45
end;
46
47
procedure TMenu.Add(aMenuComponent: TMenuComponent);
48
begin
49
FMenuComponents.Add(aMenuComponent);
50
end;
51
52
procedure TMenu.Remove(aMenuComponent: TMenuComponent);
53
begin
54
FMenuComponents.Remove(aMenuComponent);
55
end;
56
57
function TMenu.GetChild(i: Integer): TMenuComponent;
58
begin
59
Result := TMenuComponent(FMenuComponents.Items[i]);
60
end;
61
62
function TMenu.GetDescription: string;
63
begin
64
Result := FDescription;
65
end;
66
67
function TMenu.GetName: string;
68
begin
69
Result := FName;
70
end;
71
72
procedure TMenu.Print;
73
var
74
MenuComponent: Pointer;
75
begin
76
Write(GetName);
77
Writeln(‘, ‘ + GetDescription);
78
Writeln(‘-------------------‘);
79
80
for MenuComponent in FMenuComponents do
81
TMenuComponent(MenuComponent).Print;
82
end;
83
84
end.
![]()
1
2
{《HeadFirst设计模式》之组合模式 }
3
{ 组合的用户,女招待只需认识 TMenuComponent 即可。}
4
{ 编译工具:Delphi2007 for win32 }
5
{ E-Mail :guzh-0417@163.com }
6
7
unit uWaitress;
8
9
interface
10
11
uses
12
uMenuComponent;
13
14
type
15
TWaitress = class(TObject)
16
private
17
FAllMenus: TMenuComponent;
18
public
19
constructor Create(aAllMenus: TMenuComponent);
20
procedure PrintMenu;
21
end;
22
23
implementation
24
25
{ TWaitress }
26
27
constructor TWaitress.Create(aAllMenus: TMenuComponent);
28
begin
29
FAllMenus := aAllMenus;
30
end;
31
32
procedure TWaitress.PrintMenu;
33
begin
34
FAllMenus.Print;
35
end;
36
37
end.
![]()
1
2
{《HeadFirst设计模式》之组合模式 }
3
{ 客户端 }
4
{ 编译工具:Delphi2007 for win32 }
5
{ E-Mail :guzh-0417@163.com }
6
7
program pMenuTestDrive;
8
9
{$APPTYPE CONSOLE}
10
11
uses
12
SysUtils,
13
uMenuComponent in ‘uMenuComponent.pas‘,
14
uMenuItem in ‘uMenuItem.pas‘,
15
uMenu in ‘uMenu.pas‘,
16
uWaitress in ‘uWaitress.pas‘;
17
18
var
19
PancakeHouseMenu: TMenuComponent;
20
DinerMenu: TMenuComponent;
21
CafeMenu: TMenuComponent;
22
CoffeeMenu: TMenuComponent;
23
DessertMenu: TMenuComponent;
24
25
AllMenus: TMenuComponent;
26
27
Waitress: TWaitress;
28
29
begin
30
PancakeHouseMenu := TMenu.Create(‘PANCAKE HOUSE MENU‘, ‘Breakfast‘);
31
DinerMenu := TMenu.Create(‘DINER MENU‘, ‘Lunch‘);
32
CafeMenu := TMenu.Create(‘CAFE MENU‘, ‘Dinner‘);
33
CoffeeMenu := TMenu.Create(‘COFFEE MENU‘, ‘Stuff to go with your afternoon coffee‘);
34
DessertMenu := TMenu.Create(‘DESSERT MENU‘, ‘Dessert of course!‘);
35
36
AllMenus := TMenu.Create(‘ALL MENUS‘, ‘All menus combined‘);
37
38
39
AllMenus.Add(PancakeHouseMenu);
40
AllMenus.Add(DinerMenu);
41
AllMenus.Add(CafeMenu);
42
43
PancakeHouseMenu.add(TMenuItem.Create(
44
‘K&B‘‘s Pancake Breakfast‘,
45
‘Pancakes with scrambled eggs, and toast‘,
46
True,
47
299));
48
49
PancakeHouseMenu.add(TMenuItem.Create(
50
‘Regular Pancake Breakfast‘,
51
‘Pancakes with fried eggs, sausage‘,
52
False,
53
299));
54
55
PancakeHouseMenu.add(TMenuItem.Create(
56
‘Blueberry Pancakes‘,
57
‘Pancakes made with fresh blueberries, and blueberry syrup‘,
58
True,
59
349));
60
61
PancakeHouseMenu.add(TMenuItem.Create(
62
‘Waffles‘,
63
‘Waffles, with your choice of blueberries or strawberries‘,
64
True,
65
359));
66
67
68
DinerMenu.add(TMenuItem.Create(
69
‘Vegetarian BLT‘,
70
‘(Fakin‘‘) Bacon with lettuce & tomato on whole wheat‘,
71
True,
72
299));
73
74
DinerMenu.add(TMenuItem.Create(
75
‘BLT‘,
76
‘Bacon with lettuce & tomato on whole wheat‘,
77
False,
78
299));
79
80
DinerMenu.add(TMenuItem.Create(
81
‘Soup of the day‘,
82
‘A bowl of the soup of the day, with a side of potato salad‘,
83
False,
84
329));
85
86
DinerMenu.add(TMenuItem.Create(
87
‘Hotdog‘,
88
‘A hot dog, with saurkraut, relish, onions, topped with cheese‘,
89
False,
90
305));
91
92
DinerMenu.add(TMenuItem.Create(
93
‘Steamed Veggies and Brown Rice‘,
94
‘Steamed vegetables over brown rice‘,
95
True,
96
399));
97
98
DinerMenu.Add(TMenuItem.Create(
99
‘Pasta‘,
100
‘Spaghetti with Marinara Sauce, and a slice of sourdough bread‘,
101
True,
102
389));
103
104
DinerMenu.add(dessertMenu);
105
106
107
DessertMenu.add(TMenuItem.Create(
108
‘Apple Pie‘,
109
‘Apple pie with a flakey crust, topped with vanilla icecream‘,
110
True,
111
159));
112
113
DessertMenu.add(TMenuItem.Create(
114
‘Cheesecake‘,
115
‘Creamy New York cheesecake, with a chocolate graham crust‘,
116
True,
117
199));
118
119
DessertMenu.add(TMenuItem.Create(
120
‘Sorbet‘,
121
‘A scoop of raspberry and a scoop of lime‘,
122
True,
123
189));
124
125
126
CafeMenu.add(TMenuItem.Create(
127
‘Veggie Burger and Air Fries‘,
128
‘Veggie burger on a whole wheat bun, lettuce, tomato, and fries‘,
129
True,
130
399));
131
132
CafeMenu.add(TMenuItem.Create(
133
‘Soup of the day‘,
134
‘A cup of the soup of the day, with a side salad‘,
135
False,
136
369));
137
138
CafeMenu.add(TMenuItem.Create(
139
‘Burrito‘,
140
‘A large burrito, with whole pinto beans, salsa, guacamole‘,
141
True,
142
429));
143
144
CafeMenu.add(CoffeeMenu);
145
146
147
CoffeeMenu.add(TMenuItem.Create(
148
‘Coffee Cake‘,
149
‘Crumbly cake topped with cinnamon and walnuts‘,
150
True,
151
159));
152
153
CoffeeMenu.add(TMenuItem.Create(
154
‘Bagel‘,
155
‘Flavors include sesame, poppyseed, cinnamon raisin, pumpkin‘,
156
False,
157
69));
158
159
CoffeeMenu.add(TMenuItem.Create(
160
‘Biscotti‘,
161
‘Three almond or hazelnut biscotti cookies‘,
162
True,
163
89));
164
165
Waitress := TWaitress.Create(AllMenus);
166
Waitress.PrintMenu;
167
168
AllMenus.Free;
169
Waitress.Free;
170
171
Readln;
172
end.
运行结果:
文章标题:Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---组合模式之Menus[转]
文章链接:http://soscw.com/index.php/essay/35323.html