Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---组合模式之Menus[转]

2020-12-13 15:40

阅读:334

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

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

 

soscw.com,搜素材
 1soscw.com,搜素材
 2soscw.com,搜素材{《HeadFirst设计模式》之组合模式 }
 3soscw.com,搜素材{ 单项类                        }
 4soscw.com,搜素材{ 编译工具:Delphi2007 for win32 }
 5soscw.com,搜素材{ E-Mail :guzh-0417@163.com    }
 6soscw.com,搜素材
 7soscw.com,搜素材unit uMenuItem;
 8soscw.com,搜素材
 9soscw.com,搜素材interface
10soscw.com,搜素材
11soscw.com,搜素材uses
12soscw.com,搜素材  uMenuComponent;
13soscw.com,搜素材
14soscw.com,搜素材type
15soscw.com,搜素材  TMenuItem = class(TMenuComponent)
16soscw.com,搜素材  private
17soscw.com,搜素材    FName : string;
18soscw.com,搜素材    FDescription: string;
19soscw.com,搜素材    FVegetarian : Boolean;
20soscw.com,搜素材    FPrice: Integer;
21soscw.com,搜素材  public
22soscw.com,搜素材    constructor Create(aName, aDescription: string;
23soscw.com,搜素材                       aVegetarian : Boolean;
24soscw.com,搜素材                       aPrice: Integer);
25soscw.com,搜素材    function GetName: string; override;
26soscw.com,搜素材    function GetDescription: string; override;
27soscw.com,搜素材    function GetPrice: Integer; override;
28soscw.com,搜素材    function IsVegetarian: Boolean; override;
29soscw.com,搜素材    procedure Print; override;
30soscw.com,搜素材  end;
31soscw.com,搜素材
32soscw.com,搜素材implementation
33soscw.com,搜素材
34soscw.com,搜素材{ TMenuItem }
35soscw.com,搜素材
36soscw.com,搜素材constructor TMenuItem.Create(aName, aDescription: string;
37soscw.com,搜素材                             aVegetarian: Boolean;
38soscw.com,搜素材                             aPrice: Integer);
39soscw.com,搜素材begin
40soscw.com,搜素材  FName  := aName;
41soscw.com,搜素材  FDescription := aDescription;
42soscw.com,搜素材  FVegetarian  := aVegetarian;
43soscw.com,搜素材  FPrice := aPrice;
44soscw.com,搜素材end;
45soscw.com,搜素材
46soscw.com,搜素材function TMenuItem.GetDescription: string;
47soscw.com,搜素材begin
48soscw.com,搜素材  Result := FDescription
49soscw.com,搜素材end;
50soscw.com,搜素材
51soscw.com,搜素材function TMenuItem.GetName: string;
52soscw.com,搜素材begin
53soscw.com,搜素材  Result := FName;
54soscw.com,搜素材end;
55soscw.com,搜素材
56soscw.com,搜素材function TMenuItem.GetPrice: Integer;
57soscw.com,搜素材begin
58soscw.com,搜素材  Result := FPrice;
59soscw.com,搜素材end;
60soscw.com,搜素材
61soscw.com,搜素材function TMenuItem.IsVegetarian: Boolean;
62soscw.com,搜素材begin
63soscw.com,搜素材  Result := FVegetarian
64soscw.com,搜素材end;
65soscw.com,搜素材
66soscw.com,搜素材procedure TMenuItem.Print;
67soscw.com,搜素材begin
68soscw.com,搜素材  Write(‘ ‘ + GetName);
69soscw.com,搜素材  if IsVegetarian then
70soscw.com,搜素材  begin
71soscw.com,搜素材    Write(‘(V)‘);
72soscw.com,搜素材  end;
73soscw.com,搜素材  Writeln(‘, ‘, GetPrice);
74soscw.com,搜素材  Writeln(‘     --‘ + GetDescription);
75soscw.com,搜素材end;
76soscw.com,搜素材
77soscw.com,搜素材end.

 

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

 

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

 

soscw.com,搜素材
  1soscw.com,搜素材
  2soscw.com,搜素材{《HeadFirst设计模式》之组合模式 }
  3soscw.com,搜素材{ 客户端                         }
  4soscw.com,搜素材{ 编译工具:Delphi2007 for win32 }
  5soscw.com,搜素材{ E-Mail :guzh-0417@163.com     }
  6soscw.com,搜素材
  7soscw.com,搜素材program pMenuTestDrive;
  8soscw.com,搜素材
  9soscw.com,搜素材{$APPTYPE CONSOLE}
 10soscw.com,搜素材
 11soscw.com,搜素材uses
 12soscw.com,搜素材  SysUtils,
 13soscw.com,搜素材  uMenuComponent in ‘uMenuComponent.pas‘,
 14soscw.com,搜素材  uMenuItem in ‘uMenuItem.pas‘,
 15soscw.com,搜素材  uMenu in ‘uMenu.pas‘,
 16soscw.com,搜素材  uWaitress in ‘uWaitress.pas‘;
 17soscw.com,搜素材
 18soscw.com,搜素材var
 19soscw.com,搜素材  PancakeHouseMenu: TMenuComponent;
 20soscw.com,搜素材  DinerMenu: TMenuComponent;
 21soscw.com,搜素材  CafeMenu: TMenuComponent;
 22soscw.com,搜素材  CoffeeMenu: TMenuComponent;
 23soscw.com,搜素材  DessertMenu: TMenuComponent;
 24soscw.com,搜素材
 25soscw.com,搜素材  AllMenus: TMenuComponent;
 26soscw.com,搜素材
 27soscw.com,搜素材  Waitress: TWaitress;
 28soscw.com,搜素材  
 29soscw.com,搜素材begin
 30soscw.com,搜素材  PancakeHouseMenu := TMenu.Create(‘PANCAKE HOUSE MENU‘, ‘Breakfast‘);
 31soscw.com,搜素材  DinerMenu := TMenu.Create(‘DINER MENU‘, ‘Lunch‘);
 32soscw.com,搜素材  CafeMenu := TMenu.Create(‘CAFE MENU‘, ‘Dinner‘);
 33soscw.com,搜素材  CoffeeMenu := TMenu.Create(‘COFFEE MENU‘, ‘Stuff to go with your afternoon coffee‘);
 34soscw.com,搜素材  DessertMenu := TMenu.Create(‘DESSERT MENU‘, ‘Dessert of course!‘);
 35soscw.com,搜素材
 36soscw.com,搜素材  AllMenus := TMenu.Create(‘ALL MENUS‘, ‘All menus combined‘);
 37soscw.com,搜素材
 38soscw.com,搜素材
 39soscw.com,搜素材  AllMenus.Add(PancakeHouseMenu);
 40soscw.com,搜素材  AllMenus.Add(DinerMenu);
 41soscw.com,搜素材  AllMenus.Add(CafeMenu);
 42soscw.com,搜素材
 43soscw.com,搜素材  PancakeHouseMenu.add(TMenuItem.Create(
 44soscw.com,搜素材    ‘K&B‘‘s Pancake Breakfast‘,
 45soscw.com,搜素材    ‘Pancakes with scrambled eggs, and toast‘,
 46soscw.com,搜素材    True,
 47soscw.com,搜素材    299));
 48soscw.com,搜素材
 49soscw.com,搜素材  PancakeHouseMenu.add(TMenuItem.Create(
 50soscw.com,搜素材    ‘Regular Pancake Breakfast‘,
 51soscw.com,搜素材    ‘Pancakes with fried eggs, sausage‘,
 52soscw.com,搜素材    False,
 53soscw.com,搜素材    299));
 54soscw.com,搜素材
 55soscw.com,搜素材  PancakeHouseMenu.add(TMenuItem.Create(
 56soscw.com,搜素材    ‘Blueberry Pancakes‘,
 57soscw.com,搜素材    ‘Pancakes made with fresh blueberries, and blueberry syrup‘,
 58soscw.com,搜素材    True,
 59soscw.com,搜素材    349));
 60soscw.com,搜素材
 61soscw.com,搜素材  PancakeHouseMenu.add(TMenuItem.Create(
 62soscw.com,搜素材    ‘Waffles‘,
 63soscw.com,搜素材    ‘Waffles, with your choice of blueberries or strawberries‘,
 64soscw.com,搜素材    True,
 65soscw.com,搜素材    359));
 66soscw.com,搜素材
 67soscw.com,搜素材
 68soscw.com,搜素材  DinerMenu.add(TMenuItem.Create(
 69soscw.com,搜素材    ‘Vegetarian BLT‘,
 70soscw.com,搜素材    ‘(Fakin‘‘) Bacon with lettuce & tomato on whole wheat‘,
 71soscw.com,搜素材    True,
 72soscw.com,搜素材    299));
 73soscw.com,搜素材
 74soscw.com,搜素材  DinerMenu.add(TMenuItem.Create(
 75soscw.com,搜素材    ‘BLT‘,
 76soscw.com,搜素材    ‘Bacon with lettuce & tomato on whole wheat‘,
 77soscw.com,搜素材    False,
 78soscw.com,搜素材    299));
 79soscw.com,搜素材
 80soscw.com,搜素材  DinerMenu.add(TMenuItem.Create(
 81soscw.com,搜素材    ‘Soup of the day‘,
 82soscw.com,搜素材    ‘A bowl of the soup of the day, with a side of potato salad‘,
 83soscw.com,搜素材    False,
 84soscw.com,搜素材    329));
 85soscw.com,搜素材
 86soscw.com,搜素材  DinerMenu.add(TMenuItem.Create(
 87soscw.com,搜素材    ‘Hotdog‘,
 88soscw.com,搜素材    ‘A hot dog, with saurkraut, relish, onions, topped with cheese‘,
 89soscw.com,搜素材    False,
 90soscw.com,搜素材    305));
 91soscw.com,搜素材
 92soscw.com,搜素材  DinerMenu.add(TMenuItem.Create(
 93soscw.com,搜素材    ‘Steamed Veggies and Brown Rice‘,
 94soscw.com,搜素材    ‘Steamed vegetables over brown rice‘,
 95soscw.com,搜素材    True,
 96soscw.com,搜素材    399));
 97soscw.com,搜素材
 98soscw.com,搜素材  DinerMenu.Add(TMenuItem.Create(
 99soscw.com,搜素材    ‘Pasta‘,
100soscw.com,搜素材    ‘Spaghetti with Marinara Sauce, and a slice of sourdough bread‘,
101soscw.com,搜素材    True,
102soscw.com,搜素材    389));
103soscw.com,搜素材
104soscw.com,搜素材  DinerMenu.add(dessertMenu);
105soscw.com,搜素材
106soscw.com,搜素材
107soscw.com,搜素材  DessertMenu.add(TMenuItem.Create(
108soscw.com,搜素材    ‘Apple Pie‘,
109soscw.com,搜素材    ‘Apple pie with a flakey crust, topped with vanilla icecream‘,
110soscw.com,搜素材    True,
111soscw.com,搜素材    159));
112soscw.com,搜素材
113soscw.com,搜素材  DessertMenu.add(TMenuItem.Create(
114soscw.com,搜素材    ‘Cheesecake‘,
115soscw.com,搜素材    ‘Creamy New York cheesecake, with a chocolate graham crust‘,
116soscw.com,搜素材    True,
117soscw.com,搜素材    199));
118soscw.com,搜素材
119soscw.com,搜素材  DessertMenu.add(TMenuItem.Create(
120soscw.com,搜素材    ‘Sorbet‘,
121soscw.com,搜素材    ‘A scoop of raspberry and a scoop of lime‘,
122soscw.com,搜素材    True,
123soscw.com,搜素材    189));
124soscw.com,搜素材
125soscw.com,搜素材
126soscw.com,搜素材  CafeMenu.add(TMenuItem.Create(
127soscw.com,搜素材    ‘Veggie Burger and Air Fries‘,
128soscw.com,搜素材    ‘Veggie burger on a whole wheat bun, lettuce, tomato, and fries‘,
129soscw.com,搜素材    True,
130soscw.com,搜素材    399));
131soscw.com,搜素材
132soscw.com,搜素材  CafeMenu.add(TMenuItem.Create(
133soscw.com,搜素材    ‘Soup of the day‘,
134soscw.com,搜素材    ‘A cup of the soup of the day, with a side salad‘,
135soscw.com,搜素材    False,
136soscw.com,搜素材    369));
137soscw.com,搜素材
138soscw.com,搜素材  CafeMenu.add(TMenuItem.Create(
139soscw.com,搜素材    ‘Burrito‘,
140soscw.com,搜素材    ‘A large burrito, with whole pinto beans, salsa, guacamole‘,
141soscw.com,搜素材    True,
142soscw.com,搜素材    429));
143soscw.com,搜素材
144soscw.com,搜素材  CafeMenu.add(CoffeeMenu);
145soscw.com,搜素材
146soscw.com,搜素材
147soscw.com,搜素材  CoffeeMenu.add(TMenuItem.Create(
148soscw.com,搜素材    ‘Coffee Cake‘,
149soscw.com,搜素材    ‘Crumbly cake topped with cinnamon and walnuts‘,
150soscw.com,搜素材    True,
151soscw.com,搜素材    159));
152soscw.com,搜素材
153soscw.com,搜素材  CoffeeMenu.add(TMenuItem.Create(
154soscw.com,搜素材    ‘Bagel‘,
155soscw.com,搜素材    ‘Flavors include sesame, poppyseed, cinnamon raisin, pumpkin‘,
156soscw.com,搜素材    False,
157soscw.com,搜素材    69));
158soscw.com,搜素材
159soscw.com,搜素材  CoffeeMenu.add(TMenuItem.Create(
160soscw.com,搜素材    ‘Biscotti‘,
161soscw.com,搜素材    ‘Three almond or hazelnut biscotti cookies‘,
162soscw.com,搜素材    True,
163soscw.com,搜素材    89));
164soscw.com,搜素材
165soscw.com,搜素材  Waitress := TWaitress.Create(AllMenus);
166soscw.com,搜素材  Waitress.PrintMenu;
167soscw.com,搜素材
168soscw.com,搜素材  AllMenus.Free;
169soscw.com,搜素材  Waitress.Free;
170soscw.com,搜素材
171soscw.com,搜素材  Readln;
172soscw.com,搜素材end.


运行结果:

soscw.com,搜素材

Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---组合模式之Menus[转]

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

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


评论


亲,登录后才可以留言!