command pattern -- 命令模式
在软件构建过程中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”。但在某些场合,比如需要对行为进行“记录、撤销/重做(undo/redo)、事务”等处理,这种无法抵御变化的紧耦合是不合适的。Command设计模式就是在这种情况下,将“行为请求者”与“行为实现者”解耦,将一组行为抽象为对象,以实现二者之间的松耦合。“Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.” – GoF将一个请求封装为一个对象,从而可用不同的请求(一个被封装成了对象的请求)对客户程序(即调用者)进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。www.zzzyk.com实例分析,实际工作中,我们遇到了很多文档,都支持回退/前进的功能,它们快捷键也是惊人的相似Ctrl+z与Ctrl+y。下面将用一段代码来实现其操作,其原理就是命令模式。[cpp] www.zzzyk.com#include <iostream>#include <stack>class Receiver{private:std::string str;public:std::string get_str(){ return str; }void set_str(const std::string& s){ str=s; }void append(std::string astr){ str.append(astr); }void show(){ std::cout<<str<<std::endl; }};class Command{public:virtual void execute()=0;Command(Receiver *re,const std::string& p):r(re),par(p){}virtual ~Command(){}protected:Receiver * r;std::string par;};class UndoCommand:public Command{public:UndoCommand(Receiver *re,const std::string& p):Command(re,p){}virtual void undo()=0;virtual void redo()=0;virtual ~UndoCommand(){}};class ConcreteCommand:public UndoCommand{public:ConcreteCommand(Receiver *re,const std::string& p):UndoCommand(re,p){}void execute(){preStr=r->get_str();r->append(par);curStr=r->get_str();}void undo(){r->set_str(preStr);}void redo(){r->set_str(curStr);}private:std::string preStr;std::string curStr;};class ConcreteManager{private:std::stack<Command*> exeComStack;std::stack<Command*> undoComStack;public:void executeCommand(Command* cmd);void undoCommand();void redoCommand();~ConcreteManager();};ConcreteManager::~ConcreteManager(){while(exeComStack.size()>0){delete exeComStack.top();exeComStack.pop();}while(undoComStack.size()>0){delete undoComStack.top();undoComStack.pop();}}void ConcreteManager::executeCommand(Command* cmd){cmd->execute();exeComStack.push(cmd);}void ConcreteManager::undoCommand(){if(exeComStack.size()>0){UndoCommand *cmd =dynamic_cast<UndoCommand*>(exeComStack.top());exeComStack.pop();cmd->undo();undoComStack.push(cmd);}}void ConcreteManager::redoCommand(){if(undoComStack.size()>0){UndoCommand * cmd=dynamic_cast<UndoCommand*>(undoComStack.top());undoComStack.pop();cmd->redo();exeComStack.push(cmd);}}int main(int argc,char** argv){ConcreteManager *co=new ConcreteManager;Receiver* re=new Receiver;Command* cm1=new ConcreteCommand(re,"hu");Command* cm2=new ConcreteCommand(re,"cs");Command* cm3=new ConcreteCommand(re,"dn");Command* cm4=new ConcreteCommand(re,"!!");co->executeCommand(cm1);co->executeCommand(cm2);co->executeCommand(cm3);co->executeCommand(cm4);re->show();co->undoCom补充:软件开发 , C++ ,
上一个:SDUT 1451 括号东东
下一个:最大连续子序列 简单dp
- 更多C/C++疑问解答:
- 关于c++的cout输出的问题。
- 在学校里学过C和C++,不过学的很一般,现在自学C#,会不会很难?
- 全国计算机二级C语言笔试题
- 已知某树有2个2度结点,3个3度结点,4个4度结点,问有几个叶子结点?
- c++数据结构内部排序问题,整数排序
- 2012九月计算机二级C语言全国题库,,急求急求
- 如果assert只有一个字符串作为参数,是什么意思呢?
- C语言中,哪些运算符具有左结合性,哪些具有右结合性,帮忙总结下,谢谢了!
- 为什么用结构体编写的程序输入是,0输不出来啊~~~
- 将IEEE—754的十六进制转化为十进制浮点类型,用C或C++都行,多谢各位大侠啊,非常感谢!
- 为什么这个程序求不出公式?
- 这个链表倒置的算法请大家分析下
- c语言函数库调用
- C语言unsigned int纠错
- C语言快排求解啊