Meaningless Drivel is fun!*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Observer and Command Patterns 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 "Observer and Command Patterns" Watch "Observer and Command Patterns" New topic
Author

Observer and Command Patterns

Edmund Yong
Ranch Hand

Joined: Nov 16, 2003
Posts: 164
Hello,
I find that the Command and Observer patterns are really the same thing. The Command has an Invoker, which is equivalent to the Subject in the Observer pattern. The Command and ConcreteCommand are equivalent to Observer and ConcreteObserver. The obvious difference I find is that there is only one ConcreteCommand attached to an Invoker, whereas we can have one or more ConcreteObservers attached to a Subject. Therefore, for the following code, which pattern am I using?
Button button = new Button("OK");
button.addActionListener(new MyActionListener());
...
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked");
}
}
So am I correct to say that it is the Command pattern? Am I also correct to say that it is the Observer pattern?


SCJP 1.2, SCWCD 1.4
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The difference between Command and Observer is less in structure (in fact both look quite similar to State/Strategy), but in intention.
Command is used to model an action you need more to do with than just execute it now. You can hand commands around, remember them in a list to be able to do them again in the correct order, you can add an undo action etc. pp.
Observer is used when the Observable needs to notify other objects about certain events, but doesn't know *which* objects to notify.
Imagine an editor with undo/redo menu entries. Every time you select the menu entry, some action should took place, but you don't want the menu to be coupled to those actions. So you use the Observer pattern to notify the action every time the menu item is choosen by the user.
The editor probably also holds a list (or stack) of things the user has done to the document. Now if the UndoManager gets notified that the undo menu entry got selected, it can get the last Command from the stack and tell it to undo itself. Next time the menu entry is choosen, it will do the same to the *next* Command on the stack, etc.
I hope this makes the difference a little bit more clear...


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
Neat question and great observation about "intention." I have made observers that have commands they execute. But there were two classes and I saw them as separate things. I think there's actually a code snippet HERE
My latest conspicuous command had no observer aspect at all ... a web server receives a POST, uses a hidden field as a HashMap key to get a command and executes the command. (Unless someone wants to say a ServerSocket observes a port?)
[ December 13, 2003: Message edited by: Stan James ]

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
Antoni Mysliborski
Ranch Hand

Joined: May 13, 2011
Posts: 55
Sorry for bringing out such old thread, but I googled it out when thinking about the same problem, especially that I found separate articles, which showed Swing event handling as examples both of Command and Observer patterns, which made me confused in beginning.
After some thought I came into conclusion, that in fact they are in some way complementary. In event handling in Swing/GWT etc. Observer deals with how EventSource is coupled with EventListener (which is not solved by the Command pattern) and Command deals with how to carry on after being notified about the event (about what Observer doesn't says anything).
Therefore in case of Struts action, we have just Command pattern, but in Swing we have them both together.

Am I right?
 
jQuery in Action, 2nd edition
 
subject: Observer and Command Patterns