Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 310
HDave Avatar asked Jul 14 '10 04:07

HDave


People also ask

Why should I use Command pattern?

The Command pattern has the following advantages: It decouples the classes that invoke the operation from the object that knows how to execute the operation. It allows you to create a sequence of commands by providing a queue system.

What kind of functionality can benefit from the Command pattern?

Advantage of command patternIt separates the object that invokes the operation from the object that actually performs the operation. It makes easy to add new commands, because existing classes remain unchanged.

What is the motivation behind using the Command design pattern?

The main motivation for using the Command pattern is that the executor of the command does not need to know anything at all about what the command is, what context information it needs on or what it does. All of that is encapsulated in the command.

What is Command pattern in Java?

Simply put, the pattern intends to encapsulate in an object all the data required for performing a given action (command), including what method to call, the method's arguments, and the object to which the method belongs.


1 Answers

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

like image 102
Chris Lercher Avatar answered Oct 06 '22 00:10

Chris Lercher