This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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 ]
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.
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
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