who可以给我一个command模式的例子,附带讲解一下!thanks(50分)

F

foxrun

Unregistered / Unconfirmed
GUEST, unregistred user!
who可以给我一个command模式的例子,附带讲解一下!thanks
 
你把问题提的明白点好么,我没搞清楚你要干啥?把你的问题提的具体点
 
搂主已经提得很具体拉。
//======================这个是那本书里的
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;
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.
 
command模式使命令的发出者和命令的执行者隔离开来,
两者的变化互相不影响。
定义一个接口
ICommand=interface
procedure Execute;
end
再声明一个类实现该接口
TCommand=class(TObject,ICommand)
procedure Execute;
end;
procedure TCommand.Execute;
begin
....
end;
在另外一个地方,只用知道ICommand这个接口声明就可以用了
比如定义一个变量aCommand:ICommand;
接收一个TCommand对象的赋值,然后aCommand.Execute可以调用。
Command模式的用法非常普遍,
有两个高级的应用:
1。实现UNDO功能
2。实现多线程模式(不是用Thread类)

 
顶部 底部