Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Observer and Command Patterns

 
Edmund Yong
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Antoni Mysliborski
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic