Why use the command pattern in GWT (or any web app)?

谁都会走 提交于 2019-12-04 10:40:42

问题


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?


回答1:


  • 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.



来源:https://stackoverflow.com/questions/3243423/why-use-the-command-pattern-in-gwt-or-any-web-app

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