实现命令模式 [英] Implementing the command pattern

查看:126
本文介绍了实现命令模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序的设计过程中,我想使用命令格式用于撤消/重做目的。我对命令模式做了一些研究,但是我唯一没有得到的是:应该有一个命令有undo和redo方法,或者我应该做两个单独的命令,一个用于undo,一个用于重做,并且从主要命令本身?

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?

推荐答案

命令对象本身应该实现撤消/重做功能。

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.

这篇关于实现命令模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆