码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---迭代器模式之DinerMenu[转]

时间:2014-11-05 16:33:55      阅读:426      评论:0      收藏:0      [点我收藏+]

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

     容器的主要职责有两个:存放元素和浏览元素。根据单一职责原则(SRP)要将二者分开,于是将浏览功能打包封装就有了迭代器。

      用迭代器封装对动态数组的遍历:

 

bubuko.com,布布扣
 1bubuko.com,布布扣
 2bubuko.com,布布扣{《HeadFirst设计模式》之迭代器模式 }
 3bubuko.com,布布扣{ 容器中的元素类                  }
 4bubuko.com,布布扣{ 编译工具:Delphi7.0             }
 5bubuko.com,布布扣{ E-Mail :guzh-0417@163.com     }
 6bubuko.com,布布扣
 7bubuko.com,布布扣unit uItem;
 8bubuko.com,布布扣
 9bubuko.com,布布扣interface
10bubuko.com,布布扣
11bubuko.com,布布扣type
12bubuko.com,布布扣  TMenuItem = class(TObject)
13bubuko.com,布布扣  private
14bubuko.com,布布扣    FName: String;
15bubuko.com,布布扣    FDescription: String;
16bubuko.com,布布扣    FVegetarian : Boolean;
17bubuko.com,布布扣    FPrice: Double;
18bubuko.com,布布扣  public
19bubuko.com,布布扣    constructor Create(aName, aDescription: String;
20bubuko.com,布布扣                       aVegetarian : Boolean;
21bubuko.com,布布扣                       aPrice: Double);
22bubuko.com,布布扣    function GetName: String;
23bubuko.com,布布扣    function GetDescription: String;
24bubuko.com,布布扣    function GetPrice: Double;
25bubuko.com,布布扣    function IsVegetarian: Boolean;
26bubuko.com,布布扣  end;
27bubuko.com,布布扣
28bubuko.com,布布扣implementation
29bubuko.com,布布扣
30bubuko.com,布布扣{ TMenuItem }
31bubuko.com,布布扣
32bubuko.com,布布扣constructor TMenuItem.Create(aName, aDescription: String;
33bubuko.com,布布扣                             aVegetarian: Boolean;
34bubuko.com,布布扣                             aPrice: Double);
35bubuko.com,布布扣begin
36bubuko.com,布布扣  FName  := aName;
37bubuko.com,布布扣  FDescription := aDescription;
38bubuko.com,布布扣  FVegetarian  := aVegetarian;
39bubuko.com,布布扣  FPrice := aPrice;
40bubuko.com,布布扣end;
41bubuko.com,布布扣
42bubuko.com,布布扣function TMenuItem.GetDescription: String;
43bubuko.com,布布扣begin
44bubuko.com,布布扣  Result := FDescription;
45bubuko.com,布布扣end;
46bubuko.com,布布扣
47bubuko.com,布布扣function TMenuItem.GetName: String;
48bubuko.com,布布扣begin
49bubuko.com,布布扣  Result := FName;
50bubuko.com,布布扣end;
51bubuko.com,布布扣
52bubuko.com,布布扣function TMenuItem.GetPrice: Double;
53bubuko.com,布布扣begin
54bubuko.com,布布扣  Result := FPrice;
55bubuko.com,布布扣end;
56bubuko.com,布布扣
57bubuko.com,布布扣function TMenuItem.IsVegetarian: Boolean;
58bubuko.com,布布扣begin
59bubuko.com,布布扣  Result := FVegetarian;
60bubuko.com,布布扣end;
61bubuko.com,布布扣
62bubuko.com,布布扣end.

 

bubuko.com,布布扣
 1bubuko.com,布布扣
 2bubuko.com,布布扣{《HeadFirst设计模式》之迭代器模式 }
 3bubuko.com,布布扣{ 迭代器:封装对容器的遍历         }
 4bubuko.com,布布扣{ 编译工具:Delphi7.0            }
 5bubuko.com,布布扣{ E-Mail :guzh-0417@163.com    }
 6bubuko.com,布布扣
 7bubuko.com,布布扣unit uIterator;
 8bubuko.com,布布扣
 9bubuko.com,布布扣interface
10bubuko.com,布布扣
11bubuko.com,布布扣uses
12bubuko.com,布布扣  uItem;
13bubuko.com,布布扣
14bubuko.com,布布扣type
15bubuko.com,布布扣  TMenuItems = array of TMenuItem;
16bubuko.com,布布扣  
17bubuko.com,布布扣  TIterator = class(TObject)
18bubuko.com,布布扣  public
19bubuko.com,布布扣    function HasNext: Boolean; virtual; abstract;
20bubuko.com,布布扣    function Next   : TObject; virtual; abstract;
21bubuko.com,布布扣  end;
22bubuko.com,布布扣
23bubuko.com,布布扣  TDinerMenuIterator = class(TIterator)
24bubuko.com,布布扣  private
25bubuko.com,布布扣    FMenuItem : TMenuItem;
26bubuko.com,布布扣    FMenuItems: TMenuItems;
27bubuko.com,布布扣    FPosition : Integer;
28bubuko.com,布布扣  public
29bubuko.com,布布扣    constructor Create(MenuItems: TMenuItems);
30bubuko.com,布布扣    function HasNext: Boolean; override;
31bubuko.com,布布扣    function Next   : TObject; override;
32bubuko.com,布布扣  end;
33bubuko.com,布布扣
34bubuko.com,布布扣implementation
35bubuko.com,布布扣
36bubuko.com,布布扣{ TDinerMenuIterator }
37bubuko.com,布布扣
38bubuko.com,布布扣constructor TDinerMenuIterator.Create(MenuItems: TMenuItems);
39bubuko.com,布布扣begin
40bubuko.com,布布扣  FMenuItems := MenuItems;
41bubuko.com,布布扣end;
42bubuko.com,布布扣
43bubuko.com,布布扣function TDinerMenuIterator.HasNext: Boolean;
44bubuko.com,布布扣begin
45bubuko.com,布布扣  if (FPosition < Length(FMenuItems)) and (FMenuItems[FPosition] <> nil) then
46bubuko.com,布布扣    Result := True
47bubuko.com,布布扣  else
48bubuko.com,布布扣    Result := False;
49bubuko.com,布布扣end;
50bubuko.com,布布扣
51bubuko.com,布布扣function TDinerMenuIterator.Next: TObject;
52bubuko.com,布布扣begin
53bubuko.com,布布扣  FMenuItem := FMenuItems[FPosition];
54bubuko.com,布布扣  FPosition := FPosition + 1 ;
55bubuko.com,布布扣  Result := FMenuItem;
56bubuko.com,布布扣end;
57bubuko.com,布布扣
58bubuko.com,布布扣end.

 

bubuko.com,布布扣
  1bubuko.com,布布扣
  2bubuko.com,布布扣{《HeadFirst设计模式》之迭代器模式 }
  3bubuko.com,布布扣{ 容器类及其用户: Waitress       }
  4bubuko.com,布布扣{ 编译工具:Delphi7.0            }
  5bubuko.com,布布扣{ E-Mail :guzh-0417@163.com    } 
  6bubuko.com,布布扣
  7bubuko.com,布布扣unit uAggregate;
  8bubuko.com,布布扣
  9bubuko.com,布布扣interface
 10bubuko.com,布布扣
 11bubuko.com,布布扣uses
 12bubuko.com,布布扣  SysUtils, uItem, uIterator;
 13bubuko.com,布布扣
 14bubuko.com,布布扣type
 15bubuko.com,布布扣  TMenu = class(TObject)
 16bubuko.com,布布扣  public
 17bubuko.com,布布扣    function CreateIterator: TIterator; virtual; abstract;
 18bubuko.com,布布扣  end;
 19bubuko.com,布布扣
 20bubuko.com,布布扣  TDinerMenu = class(TMenu)
 21bubuko.com,布布扣  private
 22bubuko.com,布布扣    FMenuItem : TMenuItem;
 23bubuko.com,布布扣    FMenuItems: TMenuItems;
 24bubuko.com,布布扣    FNumberOfItems: Integer;
 25bubuko.com,布布扣  public
 26bubuko.com,布布扣    constructor Create;
 27bubuko.com,布布扣    destructor Destroy; override;
 28bubuko.com,布布扣    procedure AddItem(aName, aDescription: String; aVegetarian: Boolean;
 29bubuko.com,布布扣                      aPrice: Double);
 30bubuko.com,布布扣    function CreateIterator: TIterator; override;
 31bubuko.com,布布扣  end;
 32bubuko.com,布布扣
 33bubuko.com,布布扣  TWaitress = class(TObject)
 34bubuko.com,布布扣  private
 35bubuko.com,布布扣    FMenuItem : TMenuItem;
 36bubuko.com,布布扣    FDinerMenu: TDinerMenu;
 37bubuko.com,布布扣    DinerIterator: TIterator;
 38bubuko.com,布布扣  public
 39bubuko.com,布布扣    constructor Create(aDinerMenu: TDinerMenu);
 40bubuko.com,布布扣    procedure PrintMenu; overload;
 41bubuko.com,布布扣    procedure PrintMenu(aIterator: TIterator); overload;
 42bubuko.com,布布扣  end;
 43bubuko.com,布布扣  
 44bubuko.com,布布扣implementation
 45bubuko.com,布布扣
 46bubuko.com,布布扣const
 47bubuko.com,布布扣  MAX_TIMES = 6;
 48bubuko.com,布布扣
 49bubuko.com,布布扣{ TDinerMenu }
 50bubuko.com,布布扣
 51bubuko.com,布布扣procedure TDinerMenu.AddItem(aName, aDescription: String; aVegetarian: Boolean;
 52bubuko.com,布布扣                             aPrice: Double);
 53bubuko.com,布布扣begin
 54bubuko.com,布布扣  FMenuItem := TMenuItem.Create(aName, aDescription, aVegetarian, aPrice);
 55bubuko.com,布布扣  if FNumberOfItems >= MAX_TIMES then
 56bubuko.com,布布扣    Writeln(‘Sorry, menu is full! Can‘‘t add item to menu‘)
 57bubuko.com,布布扣  else
 58bubuko.com,布布扣  begin
 59bubuko.com,布布扣    FMenuItems[FNumberOfItems] := FMenuItem;
 60bubuko.com,布布扣    FNumberOfItems := FNumberOfItems + 1;
 61bubuko.com,布布扣  end;
 62bubuko.com,布布扣end;
 63bubuko.com,布布扣
 64bubuko.com,布布扣constructor TDinerMenu.Create;
 65bubuko.com,布布扣begin
 66bubuko.com,布布扣  SetLength(FMenuItems, MAX_TIMES);
 67bubuko.com,布布扣  
 68bubuko.com,布布扣  AddItem(‘Vegetarian BLT‘,
 69bubuko.com,布布扣          ‘Fakin Bacon with lettuce & tomato on whole Wheat‘, True, 2.99);
 70bubuko.com,布布扣  AddItem(‘BLT‘,
 71bubuko.com,布布扣          ‘Bacon with lettuce & tomato on whole Wheat‘, False, 2.99);
 72bubuko.com,布布扣  AddItem(‘Soup of the day‘,
 73bubuko.com,布布扣          ‘Soup of the day, with a side of potato salad‘, False, 3.29);
 74bubuko.com,布布扣  AddItem(‘Hotdog‘,
 75bubuko.com,布布扣          ‘A hot dog, with saurkraut, relish, onions, topped with cheese‘,
 76bubuko.com,布布扣          False, 3.05);
 77bubuko.com,布布扣  AddItem(‘Steamed Veggies and Brown Rice‘,
 78bubuko.com,布布扣          ‘Steamed vegetables over brown rice‘, True, 3.99);
 79bubuko.com,布布扣  AddItem(‘Pasta‘,
 80bubuko.com,布布扣          ‘Spaghetti with Marinara Sauce, and a slice of sourdough bread‘, True,
 81bubuko.com,布布扣           3.89);
 82bubuko.com,布布扣end;
 83bubuko.com,布布扣
 84bubuko.com,布布扣destructor TDinerMenu.Destroy;
 85bubuko.com,布布扣begin
 86bubuko.com,布布扣  FreeAndNil(FMenuItem);
 87bubuko.com,布布扣  inherited;
 88bubuko.com,布布扣end;
 89bubuko.com,布布扣
 90bubuko.com,布布扣function TDinerMenu.CreateIterator: TIterator;
 91bubuko.com,布布扣begin
 92bubuko.com,布布扣  Result := TDinerMenuIterator.Create(FMenuItems);
 93bubuko.com,布布扣end;
 94bubuko.com,布布扣
 95bubuko.com,布布扣{ TWaitress }
 96bubuko.com,布布扣
 97bubuko.com,布布扣constructor TWaitress.Create(aDinerMenu: TDinerMenu);
 98bubuko.com,布布扣begin
 99bubuko.com,布布扣  FDinerMenu := aDinerMenu;
100bubuko.com,布布扣end;
101bubuko.com,布布扣
102bubuko.com,布布扣procedure TWaitress.PrintMenu;
103bubuko.com,布布扣begin
104bubuko.com,布布扣  try
105bubuko.com,布布扣    DinerIterator := FDinerMenu.CreateIterator;
106bubuko.com,布布扣    Writeln(‘MENU‘);
107bubuko.com,布布扣    Writeln(‘----‘);
108bubuko.com,布布扣    Writeln(‘BREAKFAST‘);
109bubuko.com,布布扣    Writeln;
110bubuko.com,布布扣    PrintMenu(DinerIterator);
111bubuko.com,布布扣  finally
112bubuko.com,布布扣    FreeAndNil(DinerIterator);
113bubuko.com,布布扣  end;
114bubuko.com,布布扣end;
115bubuko.com,布布扣
116bubuko.com,布布扣procedure TWaitress.PrintMenu(aIterator: TIterator);
117bubuko.com,布布扣begin
118bubuko.com,布布扣  while (aIterator.HasNext) do
119bubuko.com,布布扣  begin
120bubuko.com,布布扣    FMenuItem := (aIterator.Next) as TMenuItem;
121bubuko.com,布布扣    Writeln(FMenuItem.GetName + ‘,‘);
122bubuko.com,布布扣    Writeln(FMenuItem.GetPrice, ‘ -- ‘);
123bubuko.com,布布扣    Writeln(FMenuItem.GetDescription);
124bubuko.com,布布扣  end;
125bubuko.com,布布扣end;
126bubuko.com,布布扣
127bubuko.com,布布扣end.

 

bubuko.com,布布扣
 1bubuko.com,布布扣
 2bubuko.com,布布扣{《HeadFirst设计模式》之迭代器模式 }
 3bubuko.com,布布扣{ 客户端                         }
 4bubuko.com,布布扣{ 编译工具:Delphi7.0            }
 5bubuko.com,布布扣{ E-Mail :guzh-0417@163.com    }
 6bubuko.com,布布扣
 7bubuko.com,布布扣program pMenuTestDrive;
 8bubuko.com,布布扣
 9bubuko.com,布布扣{$APPTYPE CONSOLE}
10bubuko.com,布布扣
11bubuko.com,布布扣uses
12bubuko.com,布布扣  SysUtils,
13bubuko.com,布布扣  uItem in ‘uItem.pas‘,
14bubuko.com,布布扣  uAggregate in ‘uAggregate.pas‘,
15bubuko.com,布布扣  uIterator in ‘uIterator.pas‘;
16bubuko.com,布布扣
17bubuko.com,布布扣var
18bubuko.com,布布扣  DinerMenu: TDinerMenu;
19bubuko.com,布布扣  Waitress : TWaitress;
20bubuko.com,布布扣
21bubuko.com,布布扣begin
22bubuko.com,布布扣  DinerMenu := TDinerMenu.Create;
23bubuko.com,布布扣  Waitress  := TWaitress.Create(DinerMenu);
24bubuko.com,布布扣  Waitress.PrintMenu;
25bubuko.com,布布扣
26bubuko.com,布布扣  FreeAndNil(DinerMenu);
27bubuko.com,布布扣  FreeAndNil(Waitress);
28bubuko.com,布布扣  Readln;
29bubuko.com,布布扣end.

 

运行结果:

bubuko.com,布布扣

 

特别感谢:在实现上面示例时,遇到动态数组做参数的问题。感谢盒子论坛里的ZuoBaoQuan兄出手相助!

 

 
 

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---迭代器模式之DinerMenu[转]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!