⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iteratorunit2.pas

📁 设计模式delphi版给想学delphi的朋友一个很有价值的参考
💻 PAS
字号:
unit IteratorUnit2;

interface

uses Dialogs;

type

  TIterator = class;

  TItem = class
    constructor Create;
    destructor Destroy; virtual;
  end;

  TAbstractList = class
  public
    function CreateIterator(): TIterator; virtual; abstract;
  end;

  TList = class(TAbstractList)
  public
    constructor Create(anItem: TItem); overload;
    constructor Create(aList: TList); overload;
    destructor Destroy;
    function operator: TList;

    function CreateIterator(): TIterator;

    function Count(): integer;
    function Get(index: integer): TItem;
    function First(): TItem;
    function Last(): TItem;
    function Includes(const anItem: TItem): Boolean;

    procedure Append(const anItem: TItem);
    procedure Prepend(const anItem: TItem);

    procedure Remove(const anItem: TItem);
    procedure RemoveLast();
    procedure RemoveFirst();
    procedure RemoveAll();

    function Top(): TItem;
    procedure Push(const anItem: TItem);
    function Pop(): TItem;
  end;

  TIterator = class
  protected
    constructor Create;
  public
    procedure First(); virtual; abstract;
    procedure Next(); virtual; abstract;
    function IsDown(): Boolean; virtual; abstract;
    function CurrentItem(): TItem; virtual; abstract;
  end;

  TListIterator = class(TIterator)
  private
    f_List: TList;
    f_Current: integer;
  public
    constructor Create(const aList: TList);
    procedure First(); virtual;
    procedure Next(); virtual;
    function IsDown(): Boolean; virtual;
    function CurrentItem(): TItem; virtual;
  end;

  TEmployee = class(TItem)
    constructor Create;
    destructor Destroy; virtual;
    procedure print();
  end;

  TIteratorPtr = class
  private
    f_i: TIterator;
  public
    constructor Create(i: TIterator);
    destructor Destroy; virtual;
  end;

  TListTraverser = class
  private
    f_iterator: TListIterator;
  protected
    function ProcessItem(anItem: TItem): Boolean;
  public
    constructor Create(aList: TList);
    function Traverse(): Boolean;
  end;

  TPrintNEmployee = class(TListTraverser)
  private
    f_total: integer;
    f_count: integer;
  protected
    function ProcessItem(e: Temployee): boolean;
  public
    constructor Create(aList: TList; n: integer);
  end;

  TFilteringListTraverser = class
  private
    f_iterator: TListIterator;
  protected
    function ProcessItem(anItem: TItem): Boolean; virtual; abstract;
    function TestItem(anItem: TItem): boolean; virtual; abstract;
  public
    constructor Create(aList: TList);
    function Traverse(): Boolean;
  end;

procedure printEmployees(i: TIterator);

procedure print10Employee(e: TEmployee; n: Integer);

procedure print10EmployeeO(e: TEmployee; n: Integer);

implementation

constructor TItem.Create;
begin
//.....
end;

destructor TItem.Destroy;
begin
//.....
end;

constructor TList.Create(anItem: TItem);
begin
//.....
end;

constructor TList.Create(aList: TList);
begin
//.....
end;

destructor TList.Destroy;
begin
//.....
end;

function TList.operator: TList;
begin
//.....
end;

function TList.CreateIterator(): TIterator;
begin
  result := TListIterator.Create(self);
end;

function TList.Count(): integer;
begin
//.....
end;

function TList.Get(index: integer): TItem;
begin
//.....
end;

function TList.First(): TItem;
begin
//.....
end;

function TList.Last(): TItem;
begin
//.....
end;

function TList.Includes(const anItem: TItem): Boolean;
begin
//.....
end;

procedure TList.Append(const anItem: TItem);
begin
//.....
end;

procedure TList.PrePend(const anItem: TItem);
begin
//.....
end;

procedure TList.Remove(const anItem: TItem);
begin
//.....
end;

procedure TList.RemoveLast();
begin
//.....
end;

procedure TList.RemoveFirst();
begin
//.....
end;

procedure TList.RemoveAll();
begin
//.....
end;

function TList.Top(): TItem;
begin
//.....
end;

procedure TList.Push(const anItem: TItem);
begin
//.....
end;

function TList.Pop(): TItem;
begin
//.....
end;

constructor TIterator.Create;
begin
//.....
end;

constructor TListIterator.Create(const aList: TList);
begin
  f_List := aList;
  f_Current := 0;
end;

procedure TListIterator.First();
begin
  f_Current := 0;
end;

procedure TListIterator.Next();
begin
  f_Current := f_Current + 1;
end;

function TListIterator.IsDown(): Boolean;
begin
  result := (f_Current >= f_List.Count());
end;

function TListIterator.CurrentItem(): TItem;
begin
  if IsDown() then
    ShowMessage('Iterator is out of bounds.')
  else
    result := f_List.Get(f_Current);
end;

constructor TEmployee.Create;
begin
//.....
end;

destructor TEmployee.Destroy;
begin
//.....
end;

procedure TEmployee.print();
begin
//.....
end;

procedure printEmployees(i: TIterator);
begin
  i.First();
  while not i.IsDown() do
  begin
    TEmployee(i.CurrentItem()).print();
    i.Next();
  end;
end;

constructor TIteratorPtr.Create(i: TIterator);
begin
  f_i := i;
end;

destructor TIteratorPtr.Destroy;
begin
  f_i.Free;
end;

constructor TListTraverser.Create(aList: TList);
begin
  f_iterator := TListIterator.Create(aList);
end;

function TListTraverser.Traverse(): Boolean;
begin
  result := false;
  f_iterator.First();
  while not f_iterator.IsDown() do
  begin
    result := ProcessItem(f_iterator.CurrentItem());
    if Result = false then
      Break;
    f_iterator.Next();
  end;
end;

function TListTraverser.ProcessItem(anItem: TItem): Boolean;
begin
//........
end;

constructor TPrintNEmployee.Create(aList: TList; n: integer);
begin
  TListTraverser.Create(aList);
  f_count := 0;
  f_total := n;
end;

function TPrintNEmployee.ProcessItem(e: Temployee): boolean;
begin
  f_count := f_count + 1;
  e.print();
  result := (f_count < f_total);
end;

procedure print10Employee(e: TEmployee; n: Integer); //n=10
var
  employees: TList;
  pa: TPrintNEmployee;
begin
  pa := TPrintNEmployee.Create(employees.Create(e), n);
  pa.Traverse();
end;

procedure print10EmployeeO(e: TEmployee; n: Integer);
var
  employees: TList;
  i: TListIterator;
  count: integer;
begin
  i := TListIterator.Create(employees.Create(e));
  i.First();
  count := 0;
  while not i.IsDown do
  begin
    count := count + 1;
    TEmployee(i.CurrentItem()).print();
    if count >= 10 then
      Break;
  end;
end;

constructor TFilteringListTraverser.Create(aList: TList);
begin
//........
end;

function TFilteringListTraverser.Traverse(): Boolean;
begin
  result := false;
  f_iterator.First();
  while not f_iterator.IsDown() do
  begin
    if TestItem(f_iterator.CurrentItem()) then
      result := ProcessItem(f_iterator.CurrentItem());
    if Result = false then
      break;
    f_iterator.Next();
  end;
end;


end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -