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

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

问题描述

阅读本文后: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天全站免登陆