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

📄 commandunit1.pas

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

interface
uses forms, classes;

type
  TDocument = class;

  TMyApplication = class(TApplication)
  public
    procedure Add(doc: TDocument);
  end;

  TCommand = class
  protected
    constructor Create;
  public
    destructor Destroy; virtual;
    procedure Execute(); virtual; abstract;
  end;

  TOpenCommand = class(TCommand)
  private
    f_Application: TMyApplication;
    f_response: pchar;
  protected
    function AskUser(): pchar; virtual;
  public
    constructor Create(app: TMyApplication);

    procedure Execute(); virtual;
  end;

  TPasterCommand = class(TCommand)
  private
    f_document: TDocument;
  public
    constructor Create(doc: TDocument);

    procedure Execute(); virtual;
  end;

  TMacroCommand = class(TCommand)
  private
    f_cmds: TList;
  public
    constructor Create;
    destructor Destroy; virtual;

    procedure Add(cmd: TCommand); virtual;
    procedure Remove(cmd: TCommand); virtual;

    procedure Execute(); virtual;
  end;

  TDocument = class
  public
    constructor Create(n: pchar);
    procedure Open();
    procedure Close();
    procedure Cut();
    procedure Copy();
    procedure Paste();
  end;


implementation

procedure TMyApplication.Add(doc: TDocument);
begin
//.....
end;

constructor TCommand.Create;
begin
//....
end;

destructor TCommand.Destroy;
begin
//....
end;


function TOpenCommand.AskUser(): pchar;
begin
//....
end;

constructor TOpenCommand.Create(app: TMyApplication);
begin
  f_Application := app;
end;

procedure TOpenCommand.execute();
//const
//  name: pchar = AskUser();
var
  name:PChar;
  doc: TDocument;
begin
  name := AskUser;
  if Length(name) <> 0 then
  begin
    doc := TDocument.Create(name);
    f_Application.add(doc);
    doc.open;
  end;
end;

constructor TDocument.Create(n: pchar);
begin
//....
end;

procedure TDocument.Open();
begin
//....
end;

procedure TDocument.Close();
begin
//....
end;

procedure TDocument.Cut();
begin
//....
end;

procedure TDocument.Copy();
begin
//....
end;

procedure TDocument.Paste();
begin
//....
end;

constructor TPasterCommand.Create(doc: TDocument);
begin
  f_document := doc;
end;

procedure TPasterCommand.Execute();
begin
  f_document.Paste;
end;

constructor TMacroCommand.Create;
begin
//....
end;

destructor TMacroCommand.Destroy;
begin
//....
end;

procedure TMacroCommand.Add(cmd: TCommand);
begin
  f_cmds.Add(cmd);
end;

procedure TMacroCommand.Remove(cmd: TCommand);
var
  i: integer;
begin
  for i := 0 to f_cmds.Count - 1 do
    if f_cmds.Items[i] = cmd then
      f_cmds.Delete(i);
end;

procedure TMacroCommand.Execute();
var
  i: integer;
begin
  for i := 0 to f_cmds.Count - 1 do
    TCommand(f_cmds.Items[i]).Execute();
end;
end.

⌨️ 快捷键说明

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