搂主已经提得很具体拉。
//======================这个是那本书里的
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
Char;
do
c: TDocument;
begin
name := AskUser;
if Length(name) <> 0 then
begin
do
c := TDocument.Create(name);
f_Application.add(doc);
do
c.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 :=do
c;
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 - 1do
if f_cmds.Items
= cmd then
f_cmds.Delete(i);
end;
procedure TMacroCommand.Execute();
var
i: integer;
begin
for i := 0 to f_cmds.Count - 1do
TCommand(f_cmds.Items).Execute();
end;
end.