wood burning stoves*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Command pattern application of Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Command pattern application of" Watch "Command pattern application of" New topic
Author

Command pattern application of

Graham VMead
Ranch Hand

Joined: Sep 22, 2003
Posts: 154
Hi, I've seen a lot about the Command Pattern if earlier posts and understand the principal behind it.

One article I read says the applicability is

"You want to specify, queue and execute requests at different times"
"You need to support undo and change log operations"

This one mindspring says : Encapsulate commands in objects so that you can control their selection, sequencing, queue them, undo them and otherwise manipulate them.

Whilst I can see its use in these scenarios the actual usages don't seem to me to be that common a problem as warrants the patterns prominence/popularity. So I must be missing something.

The usual text book example is switching lights/fans etc on and off with on/off command objects. Remote controls etc. I've never had to program a light switch interface though!!!

Whilst I appreciate these are for clarity could you guys give real-life examples where you've used it on server side applications i.e that isn't Swing based, or Struts Actions etc. So I might be enlightened.

Thanks a lot
[ February 23, 2005: Message edited by: Graham VMead ]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
One way it can be used is for GUI's. Lets take a basic Swing GUI and use a save button as an example. You have a save button, you have a save menu item, and you also want to have a shortcut key (Ctrl+S for example). Using the command pattern you could write a SaveCommand that can be called from each of these.

When I use the Command pattern I set it up so that I have a Command Interface and a ReturnToken Interface (these would be in seperate files of course).


something like that. Now if I want the user to be able to change the shortcut key for save I can keep the shortcuts in a map to the commands. I can do command chaining (A DeleteCommand could start by useing a CheckDependencyCommand, although for that you usually have to decide how you want to merge the ReturnTokens.

At work we used the command pattern to build a fairly complex rules engine, but I don't like to get into work code outside of work.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Take a look at http://wiki.cs.uiuc.edu/PatternStories/CommandPattern


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I can go a bit into queueing. Say you have clients producing requests and a server consuming them. You web browser is such a client of course. (Not saying any web server is actually designed this way ...)

The server turns each request into a command and puts it into a queue. As it has time it pulls requests out of the queue and executes them. If requests come in very fast the queue depth grows for a while, then when requests slow down the server catches up and empties the queue. Servers often use fixed size thread pools to control just how much CPU it can put into processing commands.

This is kind of a time-shifting thing, like taping one show while watching another.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
GeeCON Prague 2014
 
subject: Command pattern application of