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?