It's not a secret anymore!
The moose likes OO, Patterns, UML and Refactoring and the fly likes Command pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Command pattern" Watch "Command pattern" New topic

Command pattern

nitinram agarwal
Ranch Hand

Joined: Jan 29, 2009
Posts: 90
I have a very basic question about the command pattern. Supose, I have an interface like

and some class implementing the interface Task

Can I say that the above code implements the command pattern?

Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 661

Try answering these questions:

1. I want to undo the task which I just executed. How will you do it?
2. Is your main program the executor of the commands. Don't you want to have some designated class to play around with the commands?
3. Doesn't your commands belong to a specific entity. Like I want the laptop to have multiple commands. Pressing a button command, pressing the power button, opening the CD tray, plugging in the power cable etc.,
4. Finally your commands work on something, like the commands I mentioned above work on the laptop. What does your commands work on?

Also do read about 'states' in commands and there are lot of internet articles to clearly explain Command pattern.
Now to answer your question, it's just polymorphism (virtual method invocation).

If you are not laughing at yourself, then you just didn't get the joke.
I agree. Here's the link:
subject: Command pattern
jQuery in Action, 3rd edition