1、命令管理器管理命令
void testCommandManagerArrangeCommand()
{
cout<<">>>>>>>>>>>>命令管理器调度命令
<<<<<<<<<<<<"<<endl;
CDeleteCommand *pCommand1=new CDeleteCommand("obj1");
CDeleteCommand *pCommand2=new CDeleteCommand("obj2");
CDeleteCommand *pCommand3=new CDeleteCommand("obj3");
CDeleteCommand *pCommand4=new CDeleteCommand("obj4");
CCommandManager manager;
manager.Do(pCommand1);
manager.Do(pCommand2);
manager.Do(pCommand3);
manager.Undo();
manager.Undo();
manager.Redo();
manager.Undo();
manager.Do(pCommand4);
manager.Undo();
manager.Redo();
}
执行效果:
>>>>>>>>>>>>命令管理器调度命令<<<<<<<<<<<<
obj1 is deleted.
obj2 is deleted.
obj3 is deleted.
obj3 is undeleted.
obj2 is undeleted.
obj2 is deleted.
obj2 is undeleted.
obj4 is deleted.
obj4 is undeleted.
obj4 is deleted.
2、命令管理器管理命令管理器
//命令管理器调度命令管理器
void testCommandManagerArrangeCommandManager()
{
cout<<">>>>>>>>>>>>命令管理器调度命令管理
器<<<<<<<<<<<<"<<endl;
CDeleteCommand *pCommand1=new CDeleteCommand("cm1--obj1");
CDeleteCommand *pCommand2=new CDeleteCommand("cm1--obj2");
CDeleteCommand *pCommand3=new CDeleteCommand("cm1--obj3");
CDeleteCommand *pCommand4=new CDeleteCommand("cm2--obj1");
CDeleteCommand *pCommand5=new CDeleteCommand("cm2--obj2");
CDeleteCommand *pCommand6=new CDeleteCommand("cm2--obj3");
CCommandManager cm1;
CCommandManager cm2;
CCommandManager cmMain;
cm1.SetParentCommandManager(&cmMain);
cm2.SetParentCommandManager(&cmMain);
cm1.Do(pCommand1);
cm2.Do(pCommand4);
cm1.Do(pCommand2);
cm2.Do(pCommand5);
cm1.Do(pCommand3);
cm2.Do(pCommand6);
cmMain.Undo();
cmMain.Undo();
cmMain.Undo();
cmMain.Redo();
cmMain.Undo();
}
执行效果:
>>>>>>>>>>>>命令管理器调度命令管理器<<<<<<<<<<<<
cm1--obj1 is deleted.
cm2--obj1 is deleted.
cm1--obj2 is deleted.
cm2--obj2 is deleted.
cm1--obj3 is deleted.
cm2--obj3 is deleted.
cm2--obj3 is undeleted.
cm1--obj3 is undeleted.
cm2--obj2 is undeleted.
cm2--obj2 is deleted.
cm2--obj2 is undeleted.
注意:
1、确保命令的Execute和UnExecute是完全可逆的操作。
2、要保障Execute和UnExecute的可逆性,特别要关注他们执行的环境(比如绘图的时候,这些操作要有同样的尺寸标准,否则很难实现互逆的操作)。
3、注意不要插入空命令。
代码
posted on 2008-03-16 23:23
littlegai 阅读(1209)
评论(0) 编辑 收藏 引用 所属分类:
我的代码玩具