1.组合与单项的抽象父类 {《HeadFirst设计模式》之组合模式 } { 组合与单项的抽象父类 } { 编译工具:Delphi2007 for win32} { E-Mail :xshlife@163.com } unit uMenuComponent; interface uses SysUtils; type TMenuComponent = class abstract(TObject) public procedure Add(aMenuComponent: TMenuComponent); virtual; procedure Remove(aMenuComponent: TMenuComponent); virtual; function GetChild(i: Integer): TMenuComponent; virtual; function GetName: string; virtual; function GetDescription: string; virtual; function GetPrice: Integer; virtual; function IsVegetarian: Boolean; virtual; procedure Print; virtual; end; implementation { TMenuComponent } procedure TMenuComponent.Add(aMenuComponent: TMenuComponent); begin raise Exception.Create('UnSupported Operation Exception!'); end; function TMenuComponent.GetChild(i: Integer): TMenuComponent; begin raise Exception.Create('UnSupported Operation Exception!'); end; function TMenuComponent.GetDescription: string; begin raise Exception.Create('UnSupported Operation Exception!'); end; function TMenuComponent.GetName: string; begin raise Exception.Create('UnSupported Operation Exception!'); end; function TMenuComponent.GetPrice: Integer; begin raise Exception.Create('UnSupported Operation Exception!'); end; function TMenuComponent.IsVegetarian: Boolean; begin raise Exception.Create('UnSupported Operation Exception!'); end; procedure TMenuComponent.Print; begin raise Exception.Create('UnSupported Operation Exception!'); end; procedure TMenuComponent.Remove(aMenuComponent: TMenuComponent); begin raise Exception.Create('UnSupported Operation Exception!'); end; end. 2.单项类 {《HeadFirst设计模式》之组合模式 } { 单项类 } { 编译工具:Delphi2007 for win32 } { E-Mail :xshlife@163.com } unit uMenuItem; interface uses uMenuComponent; type TMenuItem = class(TMenuComponent) private FName : string; FDescription: string; FVegetarian : Boolean; FPrice: Integer; public constructor Create(aName, aDescription: string; aVegetarian : Boolean; aPrice: Integer); function GetName: string; override; function GetDescription: string; override; function GetPrice: Integer; override; function IsVegetarian: Boolean; override; procedure Print; override; end; implementation { TMenuItem } constructor TMenuItem.Create(aName, aDescription: string; aVegetarian: Boolean; aPrice: Integer); begin FName := aName; FDescription := aDescription; FVegetarian := aVegetarian; FPrice := aPrice; end; function TMenuItem.GetDescription: string; begin Result := FDescription end; function TMenuItem.GetName: string; begin Result := FName; end; function TMenuItem.GetPrice: Integer; begin Result := FPrice; end; function TMenuItem.IsVegetarian: Boolean; begin Result := FVegetarian end; procedure TMenuItem.Print; begin Write(' ' + GetName); if IsVegetarian then begin Write('(V)'); end; Writeln(', ', GetPrice); Writeln(' --' + GetDescription); end; end. 3.组合类 {《HeadFirst设计模式》之组合模式 } { 组合类 } { 编译工具:Delphi2007 for win32 } { E-Mail :xshlife@163.com } unit uMenu; interface uses uMenuComponent, Classes; type TMenu = class(TMenuComponent) private FMenuComponents: TList; FName: string; FDescription: string; public constructor Create(aName, aDescription: string); destructor Destroy; override; procedure Add(aMenuComponent: TMenuComponent); override; procedure Remove(aMenuComponent: TMenuComponent); override; function GetChild(i: Integer): TMenuComponent; override; function GetName: string; override; function GetDescription: string; override; procedure Print; override; end; implementation { TMenu } constructor TMenu.Create(aName, aDescription: string); begin FMenuComponents := TList.Create; FName := aName; FDescription := aDescription; end; destructor TMenu.Destroy; begin FMenuComponents.Clear; end; procedure TMenu.Add(aMenuComponent: TMenuComponent); begin FMenuComponents.Add(aMenuComponent); end; procedure TMenu.Remove(aMenuComponent: TMenuComponent); begin FMenuComponents.Remove(aMenuComponent); end; function TMenu.GetChild(i: Integer): TMenuComponent; begin Result := TMenuComponent(FMenuComponents.Items[i]); end; function TMenu.GetDescription: string; begin Result := FDescription; end; function TMenu.GetName: string; begin Result := FName; end; procedure TMenu.Print; var MenuComponent: Pointer; begin Write(GetName); Writeln(', ' + GetDescription); Writeln('-------------------'); for MenuComponent in FMenuComponents do TMenuComponent(MenuComponent).Print; end; end. 4.组合的用户 {《HeadFirst设计模式》之组合模式 } { 组合的用户,女招待只需认识 TMenuComponent 即可。} { 编译工具:Delphi2007 for win32 } { E-Mail :xshlife@163.com } unit uWaitress; interface uses uMenuComponent; type TWaitress = class(TObject) private FAllMenus: TMenuComponent; public constructor Create(aAllMenus: TMenuComponent); procedure PrintMenu; end; implementation { TWaitress } constructor TWaitress.Create(aAllMenus: TMenuComponent); begin FAllMenus := aAllMenus; end; procedure TWaitress.PrintMenu; begin FAllMenus.Print; end; end. 5.客户端 {《HeadFirst设计模式》之组合模式 } { 客户端 } { 编译工具:Delphi2007 for win32 } { E-Mail :xshlife@163.com } program pMenuTestDrive; {$APPTYPE CONSOLE} uses SysUtils, uMenuComponent in 'uMenuComponent.pas', uMenuItem in 'uMenuItem.pas', uMenu in 'uMenu.pas', uWaitress in 'uWaitress.pas'; var PancakeHouseMenu: TMenuComponent; DinerMenu: TMenuComponent; CafeMenu: TMenuComponent; CoffeeMenu: TMenuComponent; DessertMenu: TMenuComponent; AllMenus: TMenuComponent; Waitress: TWaitress; begin PancakeHouseMenu := TMenu.Create('PANCAKE HOUSE MENU', 'Breakfast'); DinerMenu := TMenu.Create('DINER MENU', 'Lunch'); CafeMenu := TMenu.Create('CAFE MENU', 'Dinner'); CoffeeMenu := TMenu.Create('COFFEE MENU', 'Stuff to go with your afternoon coffee'); DessertMenu := TMenu.Create('DESSERT MENU', 'Dessert of course!'); AllMenus := TMenu.Create('ALL MENUS', 'All menus combined'); AllMenus.Add(PancakeHouseMenu); AllMenus.Add(DinerMenu); AllMenus.Add(CafeMenu); PancakeHouseMenu.add(TMenuItem.Create( 'K&B''s Pancake Breakfast', 'Pancakes with scrambled eggs, and toast', True, 299)); PancakeHouseMenu.add(TMenuItem.Create( 'Regular Pancake Breakfast', 'Pancakes with fried eggs, sausage', False, 299)); PancakeHouseMenu.add(TMenuItem.Create( 'Blueberry Pancakes', 'Pancakes made with fresh blueberries, and blueberry syrup', True, 349)); PancakeHouseMenu.add(TMenuItem.Create( 'Waffles', 'Waffles, with your choice of blueberries or strawberries', True, 359)); DinerMenu.add(TMenuItem.Create( 'Vegetarian BLT', '(Fakin'') Bacon with lettuce & tomato on whole wheat', True, 299)); DinerMenu.add(TMenuItem.Create( 'BLT', 'Bacon with lettuce & tomato on whole wheat', False, 299)); DinerMenu.add(TMenuItem.Create( 'Soup of the day', 'A bowl of the soup of the day, with a side of potato salad', False, 329)); DinerMenu.add(TMenuItem.Create( 'Hotdog', 'A hot dog, with saurkraut, relish, onions, topped with cheese', False, 305)); DinerMenu.add(TMenuItem.Create( 'Steamed Veggies and Brown Rice', 'Steamed vegetables over brown rice', True, 399)); DinerMenu.Add(TMenuItem.Create( 'Pasta', 'Spaghetti with Marinara Sauce, and a slice of sourdough bread', True, 389)); DinerMenu.add(dessertMenu); DessertMenu.add(TMenuItem.Create( 'Apple Pie', 'Apple pie with a flakey crust, topped with vanilla icecream', True, 159)); DessertMenu.add(TMenuItem.Create( 'Cheesecake', 'Creamy New York cheesecake, with a chocolate graham crust', True, 199)); DessertMenu.add(TMenuItem.Create( 'Sorbet', 'A scoop of raspberry and a scoop of lime', True, 189)); CafeMenu.add(TMenuItem.Create( 'Veggie Burger and Air Fries', 'Veggie burger on a whole wheat bun, lettuce, tomato, and fries', True, 399)); CafeMenu.add(TMenuItem.Create( 'Soup of the day', 'A cup of the soup of the day, with a side salad', False, 369)); CafeMenu.add(TMenuItem.Create( 'Burrito', 'A large burrito, with whole pinto beans, salsa, guacamole', True, 429)); CafeMenu.add(CoffeeMenu); CoffeeMenu.add(TMenuItem.Create( 'Coffee Cake', 'Crumbly cake topped with cinnamon and walnuts', True, 159)); CoffeeMenu.add(TMenuItem.Create( 'Bagel', 'Flavors include sesame, poppyseed, cinnamon raisin, pumpkin', False, 69)); CoffeeMenu.add(TMenuItem.Create( 'Biscotti', 'Three almond or hazelnut biscotti cookies', True, 89)); Waitress := TWaitress.Create(AllMenus); Waitress.PrintMenu; AllMenus.Free; Waitress.Free; Readln; end. 运行结果: