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

📄 iteratorunit1.pas

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

interface
uses Dialogs;

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

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

    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;

  TReverseListIterator = 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;

procedure printEmployees(i: TIterator);

implementation


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

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

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

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

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

function TList.operator: TList {=(const aList:TList)};
begin
//.....
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 TReverseListIterator.Create(const aList: TList);
begin
  f_List := aList;
  f_Current := aList.Count();
end;

procedure TReverseListIterator.First();
begin
  f_Current := f_List.Count();
end;

procedure TReverseListIterator.Next();
begin
  if not IsDown() then
    f_Current := f_Current - 1;
end;

function TReverseListIterator.IsDown(): Boolean;
begin
  result := (f_Current = 0);
end;

function TReverseListIterator.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;

end.

⌨️ 快捷键说明

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