命令模式 - 目的是什么? [英] Command Pattern - Purpose?

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

问题描述

看完这个: http://sourcemaking.com/design_patterns/command

我还是不明白为什么我们需要这个。

I still don't quite understand why we need this.

推荐答案

的想法是,如果命令被封装为对象,则这些命令可被捕获,存储,排队,重放等

The idea is that if commands are encapsulated as objects then those commands can be captured, stored, queued, replayed etc.

这也使得更容易为命令知道如何撤消本身(即执行反向操作),以便然后,如果在处理的命令,它可以被存储在一个列表中,然后撤消以相反的顺序来恢复做了这些命令之前的状态。

It also makes it easier for the command to know how to undo themselves (ie perform the reverse operation) so that then if a command is processed it can be stored in a list and then 'undone' in the reverse order to restore the state before the commands were done.

此外它分离来自接收器的命令的发送者。这可以允许多个事物以产生相同的命令(一个菜单项,并且例如一个按钮),他们会以同样的方式进行处理。

Also it decouples the sender of the command from the receiver. This can allow multiple things to generate the same command (a menu item and a button for example) and they will be handled in the same way.

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

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