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

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

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


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


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


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


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

运行结果:

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

时间: 2024-08-03 14:51:58

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

设计模式之第6章-迭代器模式(Java实现)

设计模式之第6章-迭代器模式(Java实现) “我已经过时了,就不要讲了吧,现在java自带有迭代器,还有什么好讲的呢?”“虽然已经有了,但是具体细节呢?知道实现机理岂不美哉?”“好吧好吧.”(迭代器闷闷不乐的答应下来.作者吃着小笼包,咂咂嘴道:哼,想偷懒,窗户都没有~). 迭代器模式之自我介绍 正如你们所见,我目前已经没落了,基本上没人会单独写一个迭代器,除非是产品性质的研发,我的定义如下:Provide a way to access the elements of an aggregate

.NET设计模式(18):迭代器模式(Iterator Pattern)(转)

概述 在面向对象的软件设计中,我们经常会遇到一类集合对象,这类集合对象的内部结构可能有着各种各样的实现,但是归结起来,无非有两点是需要我们去关心的:一是集合内部的数据存储结构,二是遍历集合内部的数据.面向对象设计原则中有一条是类的单一职责原则,所以我们要尽可能的去分解这些职责,用不同的类去承担不同的职责.Iterator模式就是分离了集合对象的遍历行为,抽象出一个迭代器类来负责,这样既可以做到不暴露集合的内部结构,又可让外部代码透明的访问集合内部的数据. 意图 提供一种方法顺序访问一个聚合对象中

Java设计模式(八)观察者模式 迭代器模式

(十五)观察者模式 观察者模式,定义对象间一对多关系,一个对象状态发生改变,所有依赖于它的对象都收到通知并且自动更新,观察者与被观察者分开.例如邮件订阅.RSS订阅,如果有更新就会邮件通知你. interface Observers{ public void update(); } class Observer1 implements Observers{ public void update(){ System.out.println("observer1 has received"

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---工厂模式之简单工厂

简单工厂:工厂依据传进的参数创建相应的产品. http://www.cnblogs.com/DelphiDesignPatterns/archive/2009/07/24/1530536.html {<HeadFirst设计模式>工厂模式之简单工厂 } 3{ 产品类 } 4{ 编译工具 :Delphi7.0 } 5{ 联系方式 :[email protected]com } 6 7unit uProducts; 8 9interface 10 11type 12 TPizza = class(

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---策略模式之MiniDuckSimulator[转]

 1 2{<HeadFirst设计模式>之策略模式 } 3{ 本单元中的类为策略类           } 4{ 编译工具: Delphi7.0           } 5{ E-Mail : [email protected]   } 6 7unit uStrategy; 8 9interface1011type12  {飞行接口,及其实现类 }1314  IFlyBehavior = Interface(IInterface)15    procedure Fly;16  end;1718

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---模板方法模式之CoffeineBeverageWithHook[转]

模板方法模式定义了一个算法骨架,允许子类对算法的某个或某些步骤进行重写(override).   1  2{<HeadFirst设计模式>之模板方法模式 }  3{ 编译工具: Delphi7.0              }  4{ E-Mail : [email protected]      }  5  6unit uCoffeineBeverageWithHook;  7  8interface  9 10uses 11  SysUtils; 12 13type 14  TCoffei

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之RemoteControlTest[转]

  1  2{<HeadFirst设计模式>之命令模式 }  3{ 本单元中的类为命令的接收者      }  4{ 编译工具 :Delphi7.0         }  5{ 联系方式 :[email protected] }  6  7unit uReceiveObject;  8  9interface 10 11type 12  TLight = class(TObject) 13  private 14    FLocation: String; 15  public 16    c

设计模式之十六:迭代器模式(Iterator)

迭代器模式: 提供了一种在不暴漏对象底层细节的情况下顺序访问聚合对象的方法. Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. 其实这个设计模式用的很多,但是设计的很少.因为stl中的迭代器就是这个模式的应用.估计这个设计模式的名字就是用从stl中的迭代器而来的. UML图: 主要包括: Iterator:

【java设计模式】(6)---迭代器模式(案例解析)

设计模式之迭代器模式 一.java迭代器介绍 1.迭代器接口 在jdk中,与迭代器相关的接口有两个:Iterator 与 Iterable. Iterator:迭代器,Iterator及其子类通常是迭代器本身的结构与方法:迭代器是一种模式,它可以使得对于序列类型的数据结构的遍历行为与被遍历的对象分离,即我们无需关心该序列的底层结构是什么样子的.只要拿到 这个对象,使用迭代器就可以遍历这个对象的内部. Iterable:可迭代的,那些想用到迭代器功能的其它类,如AbstractList HashM