《GOF设计模式》—备忘录(MEMENTO)—Delphi源码示例:一个反映备忘录模式的迭代接口
来源:互联网 发布:mac版qq查看群相册 编辑:程序博客网 时间:2024/04/18 21:12
说明:
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源码示例:一个反映备忘录模式的迭代接口
- 《GOF设计模式》—备忘录(MEMENTO)—Delphi源码示例:备忘录接口
- 《GOF设计模式》—备忘录(MEMENTO)—Delphi源码示例:图形编辑器
- GOF 23 设计模式之 备忘录模式(Memento)
- 设计模式的征途—20.备忘录(Memento)模式
- 设计模式——备忘录模式(Memento)
- 【设计模式】—— 备忘录模式Memento
- 设计模式—Memento备忘录模式
- JAVA设计模式—备忘录模式(Memento)
- 设计模式----Memento(备忘录)
- Memento(备忘录)设计模式
- java设计模式19——备忘录模式(Memento)
- Java设计模式——备忘录模式(Memento)
- 设计模式——备忘录(Memento Pattern)模式
- Java设计模式——备忘录模式(Memento Pattern)
- 《GOF设计模式》—迭代器 (ITERATOR)—Delphi源码示例:内部迭代器(封装不同类型的迭代)
- 备忘录模式——Memento
- 设计模式-备忘录模式(Memento)的详解
- 《GOF设计模式》—备忘录(MEMENTO)—Delphi源码示例:图形编辑器
- BCB中用Sender参数实现代码重用(修正版)
- 简单的linq to sql 的例子 ,实现了增删改查
- 使用Doxgen创建Xcode文档集
- asp.net 将数据导入到excel中 出现 “object”未包含“get_Range”的定义
- 《GOF设计模式》—备忘录(MEMENTO)—Delphi源码示例:一个反映备忘录模式的迭代接口
- google发布的gtv的js ui库
- 深入C++ Builder之编写自己的元件(1)
- 《GOF设计模式》—观察者(OBSERVER)—Delphi源码示例:观察者接口
- 马化腾周鸿祎把酒言欢
- 序列化和反序列化
- MPI并行计算环境的建立
- 表达式树
- log4j配置祥解