1
 2{《HeadFirst设计模式》之组合模式 }
 3{ 组合与单项的抽象父类           }
 4{ 编译工具:Delphi2007 for win32}
 5{ E-Mail :guzh-0417@163.com   }
 6
 7unit uMenuComponent;
 8
 9interface
10
11uses
12  SysUtils;
13
14type
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
27implementation
28
29{ TMenuComponent }
30
31procedure TMenuComponent.Add(aMenuComponent: TMenuComponent);
32begin
33  raise Exception.Create('UnSupported Operation Exception!');
34end;
35
36function TMenuComponent.GetChild(i: Integer): TMenuComponent;
37begin
38  raise Exception.Create('UnSupported Operation Exception!');
39end;
40
41function TMenuComponent.GetDescription: string;
42begin
43  raise Exception.Create('UnSupported Operation Exception!');
44end;
45
46function TMenuComponent.GetName: string;
47begin
48  raise Exception.Create('UnSupported Operation Exception!');
49end;
50
51function TMenuComponent.GetPrice: Integer;
52begin
53  raise Exception.Create('UnSupported Operation Exception!');
54end;
55
56function TMenuComponent.IsVegetarian: Boolean;
57begin
58  raise Exception.Create('UnSupported Operation Exception!');
59end;
60
61procedure TMenuComponent.Print;
62begin
63  raise Exception.Create('UnSupported Operation Exception!');
64end;
65
66procedure TMenuComponent.Remove(aMenuComponent: TMenuComponent);
67begin
68  raise Exception.Create('UnSupported Operation Exception!');
69end;
70
71end.

 


 1
 2{《HeadFirst设计模式》之组合模式 }
 3{ 单项类                        }
 4{ 编译工具:Delphi2007 for win32 }
 5{ E-Mail :guzh-0417@163.com    }
 6
 7unit uMenuItem;
 8
 9interface
10
11uses
12  uMenuComponent;
13
14type
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
32implementation
33
34{ TMenuItem }
35
36constructor TMenuItem.Create(aName, aDescription: string;
37                             aVegetarian: Boolean;
38                             aPrice: Integer);
39begin
40  FName  := aName;
41  FDescription := aDescription;
42  FVegetarian  := aVegetarian;
43  FPrice := aPrice;
44end;
45
46function TMenuItem.GetDescription: string;
47begin
48  Result := FDescription
49end;
50
51function TMenuItem.GetName: string;
52begin
53  Result := FName;
54end;
55
56function TMenuItem.GetPrice: Integer;
57begin
58  Result := FPrice;
59end;
60
61function TMenuItem.IsVegetarian: Boolean;
62begin
63  Result := FVegetarian
64end;
65
66procedure TMenuItem.Print;
67begin
68  Write(' ' + GetName);
69  if IsVegetarian then
70  begin
71    Write('(V)');
72  end;
73  Writeln(', ', GetPrice);
74  Writeln('     --' + GetDescription);
75end;
76
77end.

 


 1
 2{《HeadFirst设计模式》之组合模式   }
 3{ 组合类                         }
 4{ 编译工具:Delphi2007 for win32 }
 5{ E-Mail :guzh-0417@163.com    }
 6
 7unit uMenu;
 8
 9interface
10
11uses
12  uMenuComponent, Classes;
13
14type
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
31implementation
32
33{ TMenu }
34
35constructor TMenu.Create(aName, aDescription: string);
36begin
37  FMenuComponents := TList.Create;
38  FName := aName;
39  FDescription := aDescription;
40end;
41
42destructor TMenu.Destroy;
43begin
44  FMenuComponents.Clear;
45end;
46
47procedure TMenu.Add(aMenuComponent: TMenuComponent);
48begin
49  FMenuComponents.Add(aMenuComponent);
50end;
51
52procedure TMenu.Remove(aMenuComponent: TMenuComponent);
53begin
54  FMenuComponents.Remove(aMenuComponent);
55end;
56
57function TMenu.GetChild(i: Integer): TMenuComponent;
58begin
59  Result := TMenuComponent(FMenuComponents.Items[i]);
60end;
61
62function TMenu.GetDescription: string;
63begin
64  Result := FDescription;
65end;
66
67function TMenu.GetName: string;
68begin
69  Result := FName;
70end;
71
72procedure TMenu.Print;
73var
74  MenuComponent: Pointer;
75begin
76  Write(GetName);
77  Writeln(', ' + GetDescription);
78  Writeln('-------------------');
79
80  for MenuComponent in FMenuComponents do
81    TMenuComponent(MenuComponent).Print;
82end;
83
84end.

 


 1
 2{《HeadFirst设计模式》之组合模式                 }
 3{ 组合的用户,女招待只需认识 TMenuComponent 即可。}
 4{ 编译工具:Delphi2007 for win32                }
 5{ E-Mail :guzh-0417@163.com                   }
 6
 7unit uWaitress;
 8
 9interface
10
11uses
12  uMenuComponent;
13
14type
15  TWaitress = class(TObject)
16  private
17    FAllMenus: TMenuComponent;
18  public
19    constructor Create(aAllMenus: TMenuComponent);
20    procedure PrintMenu;
21  end;  
22
23implementation
24
25{ TWaitress }
26
27constructor TWaitress.Create(aAllMenus: TMenuComponent);
28begin
29  FAllMenus := aAllMenus;
30end;
31
32procedure TWaitress.PrintMenu;
33begin
34  FAllMenus.Print;
35end;
36
37end.

 


  1
  2{《HeadFirst设计模式》之组合模式 }
  3{ 客户端                         }
  4{ 编译工具:Delphi2007 for win32 }
  5{ E-Mail :guzh-0417@163.com     }
  6
  7program pMenuTestDrive;
  8
  9{$APPTYPE CONSOLE}
 10
 11uses
 12  SysUtils,
 13  uMenuComponent in 'uMenuComponent.pas',
 14  uMenuItem in 'uMenuItem.pas',
 15  uMenu in 'uMenu.pas',
 16  uWaitress in 'uWaitress.pas';
 17
 18var
 19  PancakeHouseMenu: TMenuComponent;
 20  DinerMenu: TMenuComponent;
 21  CafeMenu: TMenuComponent;
 22  CoffeeMenu: TMenuComponent;
 23  DessertMenu: TMenuComponent;
 24
 25  AllMenus: TMenuComponent;
 26
 27  Waitress: TWaitress;
 28  
 29begin
 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;
172end.


运行结果:



posted on 2014-11-05 14:53  小光zfg  阅读(251)  评论(0编辑  收藏  举报