Implementing the command pattern

谁说我不能喝 提交于 2019-12-18 14:48:28

问题


I am in the design process of an application, and I would like to use the command pattern for undo/redo purposes. I did some research into the command pattern but the only thing I don't get is: Should a command have the undo and redo methods, or should I make two separate commands, one for undo and one for redo, and call those from the main command itself?


回答1:


The command object itself should implement the undo / redo functionality.

The commands are generally pushed and popped from a stack maintained by a command manager to implement multi level undo. When commands are executed they are pushed onto the stack and when they are undone they are popped from the stack.

The memento pattern would be used in conjunction with the command pattern, it is not a replacement to the usage of the command pattern. It would be used to maintain the state required for the undo operation.




回答2:


You might also want to consider the memento pattern for this, we use it and it works brilliant for undo.



来源:https://stackoverflow.com/questions/2214874/implementing-the-command-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!