说明:
Dylan中的Collection提供了一个反映备忘录模式的迭代接口。Dylan的集合有一个"状态"对象的概念,它是一个表示迭代状态的备忘录。每一个集合可以按照它所选择的任意方式表示迭代的当前状态;该表示对客户完全不可见。
代码:
unit uCollection;
interface
uses Dialogs,Contnrs;
type
TItem = class
private
FValue:string;
public
constructor Create(const AValue:string);
//---
procedure Process();
end;
TIterationState = class
private
FCurrent: Integer;
public
constructor Create;
end;
TCollection = class
private
FList: TObjectList;
public
constructor Create;
destructor Destroy; override;
//---
function CreateInitialState: TIterationState;
function Copy(AState: TIterationState): TIterationState;
//---
procedure Next(AState: TIterationState);
function IsDone(AState: TIterationState): boolean;
function CurrentItem(AState: TIterationState): TObject;
//---
procedure Append(AItem: TObject);
procedure Remove(AItem: TObject);
end;
implementation
function TCollection.CreateInitialState: TIterationState;
begin
Result := TIterationState.Create;
end;
function TCollection.Copy(AState: TIterationState): TIterationState;
begin
Result := TIterationState.Create;
Result.FCurrent := AState.FCurrent;
end;
constructor TCollection.Create;
begin
FList := TObjectList.Create;
end;
destructor TCollection.Destroy;
begin
FList.Free;
//---
inherited;
end;
function TCollection.CurrentItem(AState: TIterationState): TObject;
begin
Result := FList[AState.FCurrent];
end;
function TCollection.IsDone(AState: TIterationState): boolean;
begin
Result := AState.FCurrent >= FList.Count;
end;
procedure TCollection.Next(AState: TIterationState);
begin
with AState do
FCurrent := FCurrent + 1;
end;
procedure TCollection.Append(AItem: TObject);
begin
FList.Add(AItem);
end;
procedure TCollection.Remove(AItem: TObject);
begin
FList.Remove(AItem);
end;
constructor TIterationState.Create;
begin
FCurrent := 0;
end;
constructor TItem.Create(const AValue: string);
begin
FValue := AValue;
end;
procedure TItem.Process;
begin
ShowMessage(FValue);
end;
end.
procedure TForm3.Button1Click(Sender: TObject);
var
ACollection: TCollection;
AState: TIterationState;
begin
ACollection := TCollection.Create;
try
with ACollection do
begin
Append(TItem.Create('1'));
Append(TItem.Create('2'));
Append(TItem.Create('3'));
//---
AState := CreateInitialState();
try
while not IsDone(AState) do
begin
TItem(CurrentItem(AState)).Process();
Next(AState);
end;
finally
AState.Free;
end;
end;
finally
ACollection.Free;
end;
end;
procedure TForm3.Button2Click(Sender: TObject);
var
ACollection: TCollection;
AState,AState1: TIterationState;
begin
ACollection := TCollection.Create;
try
with ACollection do
begin
Append(TItem.Create('1'));
Append(TItem.Create('2'));
Append(TItem.Create('3'));
//---
AState := CreateInitialState();
try
while not IsDone(AState) do
begin
TItem(CurrentItem(AState)).Process();
Next(AState);
//---
AState1 := Copy(AState);
try
while not IsDone(AState1) do
begin
TItem(CurrentItem(AState1)).Process();
Next(AState1);
end;
finally
AState1.Free;
end;
end;
finally
AState.Free;
end;
end;
finally
ACollection.Free;
end;
end;
《GOF设计模式》—备忘录(MEMENTO)—Delphi源码示例:一个反映备忘录模式的迭代接口
最新推荐文章于 2024-09-02 15:11:58 发布