


Definition: The Observer pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependants are notified and updated automatically.
Usage: Use this pattern when multiple classes need to be aware when an event occurs or when an event needs to be transferred from one class to another. This class is about communication between multiple objects.
interface Subject {
addObserver()
notifyObserver()
removeObserver()
}
interface Observer {
update()
}
The above example is a general sort of example. Every instance of the Observer pattern takes a form somewhat similar to what is shown above.
ActionListeners, KeyListeners, and MouseListeners are all examples of the Observer pattern. The Observer pattern is rampant throughout the Java API, particulary the AWT.
Copyright (C) 2008-2009 Steven Fletcher. All rights reserved.