笔记:Gof设计模式--Command

1、意图

  Encapsulate a request as an object, thereby letting you parameterizeclients with different requests, queue or log requests, and supportundoable operations.

 

2、适应

  Use the Command pattern when you want to
  •  parameterize objects by an action to perform, as MenuItem objects did above. You can express such rameterization in a procedural language with a callback function, that is, a function that's registered somewhere to be called at a later point. Commands are an object-oriented replacement for callbacks.

  •  specify, queue, and execute requests at different times. A Command object can have a lifetime independent of the original request. If the receiver of a request can be represented in an address space-independent way, then you can transfer a command object for the request to a different process and fulfill the request there.

  •  support undo. The Command's Execute operation can store state for reversing its effects in the command itself. The Command interface must have an added Unexecute operation that reverses the effects of a previous call to Execute. Executed commands are stored in a history list. Unlimited-level undo and redo is achieved by traversing this list backwards and forwards calling Unexecute and Execute, respectively.

  •  support logging changes so that they can be reapplied in case of a system crash. By augmenting the Command interface with load and store operations, you can keep a persistent log of changes. Recovering from a crash involves reloading logged commands from disk and reexecuting them with the Execute operation.
  •  structure a system around high-level operations built on primitives operations. Such a structure is common in information systems that support transactions. A transaction encapsulates a set of changes to data. The Command pattern offers a way to model transactions. Commands have a common interface, letting you invoke all transactions the same way. The pattern also makes it easy to extend the system with new transactions.

3、结构

 

4、示例代码

class Command { 
public: 
    virtual ~Command(); 
    virtual void Execute() = 0; 
protected: 
    Command(); 
}; 


OpenCommand opens a document whose name is supplied by theuser. An OpenCommand must be passed anApplication object in its constructor. AskUser is
animplementation routine that prompts the user for the name of thedocument to open.

class OpenCommand : public Command { 
public: 
    OpenCommand(Application*); 
    virtual void Execute(); 
protected: 
    virtual const char* AskUser(); 
private: 
    Application* _application; 
    char* _response; 
}; 
 
OpenCommand::OpenCommand (Application* a) { 
    _application = a; 
} 
 
void OpenCommand::Execute () { 
    const char* name = AskUser(); 
    if (name != 0) { 
      Document* document = new Document(name); 
      _application->Add(document); 
      document->Open(); 
    } 
} 


 

A PasteCommand must be passed a Document object asits receiver. The receiver is given as a parameter to PasteCommand'sconstructor. 

class PasteCommand : public Command { 
public: 
    PasteCommand(Document*); 
    virtual void Execute(); 
private: 
    Document* _document; 
}; 
 
PasteCommand::PasteCommand (Document* doc) {
    _document = doc;    
}        
void PasteCommand::Execute () {        
    _document->Paste();    
} 


A MacroCommand manages a sequence of subcommands and providesoperations for adding and removing subcommands. No explicit receiveris required, because the subcommands already define their receiver.

 

class MacroCommand : public Command { 
public: 
    MacroCommand(); 
    virtual ~MacroCommand(); 
    virtual void Add(Command*); 
    virtual void Remove(Command*); 
    virtual void Execute(); 
private: 
    List<Command*>* _cmds; 
};

void MacroCommand::Execute () { 
    ListIterator<Command*> i(_cmds); 
    for (i.First(); !i.IsDone(); i.Next()) { 
        Command* c = i.CurrentItem(); 
        c->Execute(); 
    } 
} 

void MacroCommand::Add (Command* c) {        
    _cmds->Append(c);    
}
 
void MacroCommand::Remove (Command* c) {        
    _cmds->Remove(c);    
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值