• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Command pattern application of

 
Graham VMead
Ranch Hand
Posts: 154
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at http://wiki.cs.uiuc.edu/PatternStories/CommandPattern
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic