为什么在GWT(或任何Web应用程序)中使用命令模式? [英] Why use the command pattern in GWT (or any web app)?
问题描述
根据此视频此处 [@ 7: 50] Google建议在其请求处理API之上使用Command模式。此外,还有一个实用模式 gwt-dispatch 的有用项目。
根据gwt-dispatch documentation 我需要为每个命令创建四个类: 假设我的服务API在8个BSOs中有100个方法,有人可以向我解释为什么我要创建近400个新类?这种模式买了多少真棒?
使用命令模式的一个很好的理由是,当你想要传递命令对象进一步委托 - 所以而不是复制所有参数,只是传递命令对象更容易。它对于gwt-dispatch的回滚功能(或者撤消/重做功能,例如在Eclipse的UndoableOperations中)也很有用。 通过使用它可以帮助提供多种命令变体不同的构造函数和命令的子类。 我不会建议总是使用这个模式,但是当您不要使用它:无论如何,您经常需要结果对象 - 并且可以重用相同的返回对象。在其他情况下,您可以为命令和结果使用相同的对象。
According to this video here [@ 7:50] Google is recommending the use of the Command pattern on top of its request handling API. There is also a helpful looking project gwt-dispatch that implements that pattern.
According to gwt-dispatch documentation I need to create four classes for each command:
- an action (e.g. command)
- a result (e.g. response)
- an action handler
- a module
Assume my service API has 100 methods across 8 BSOs, can somebody explain to me why I want to create nearly 400 new classes? What awesomeness does this pattern buy?
One good reason to use the command pattern is, when you want to pass the command object to further delegates - so instead of copying all the arguments, it's easier to just pass the command object around. It's also useful for gwt-dispatch's rollback functionality (or the undo/redo functionality e.g. in Eclipse's UndoableOperations).
It helps to provide several variations of commands by using different constructors, and subclasses of commands.
I would not suggest to always use the pattern, but you don't save as much as you think, when you don't use it: You will often need result objects anyway - and it's possible to reuse the same return objects. In other cases, you can use the same object for the command and for the result.
The module can be used for multiple commands.
这篇关于为什么在GWT(或任何Web应用程序)中使用命令模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!