Java Reference
In-Depth Information
ConcreteObserver - Implements the Observable interface and determines in each implemented method how
to respond to the message received from the Observable .
Normally the application framework registers the specific observers to the Observable .
Benefits and Drawbacks
The Observer pattern's flexibility carries with it the added benefit that the observable object can be relatively
simple. There is not a substantial amount of coding overhead. In addition, the pattern is useful for:
Testing - You can code an echo observer that can display the observable's behavior.
Incremental development - It's very easy to add additional observers as you code them.
The principal challenge for this pattern comes from implementation of the messaging model: you can use a
specific or generic message broadcast strategy. Each approach has potential disadvantages.
Generic messaging - As messaging becomes more generic, it can become more difficult to determine what is
going on for an observable component. Generic messaging can result in unnecessary message traffic; some events
could be broadcast to observers that would otherwise not care about them. Generic messaging also can result in
additional coding overhead for observers, because they have to decode messages.
Specific messaging - More-specific messages place greater coding requirements on the observable component,
since they must produce a series of notifications under specific conditions. They might also make observers more
complex, because observers must handle a variety of message types.
Pattern Variants
The Observer pattern has several variants that can be used in defining the relationship between observable and
observer:
Single observer versus multiple observers - Depending on the role of the observable component, it might
support only a single observer.
Multithreaded observable components - If an observable object is multithreaded, it can provide support for a
message queue, and can provide services such as message priority and override behavior.
Client pull - Although the pattern is oriented toward server push, you can modify it to support a limited form of
client pull. In this variant, the observable typically provides the observers with notification that an event has taken
place. If observers require more detail, they contact the observable, calling a method that requests additional
information about the event.
Related Patterns
Related patterns include Proxy (page 197). For distributed communication, the Remote Proxy pattern is often used
to manage communication between the Observer and Observable .
Example
Note:
For a full working example of this code example, with additional supporting classes and/or a RunPattern class,
see “ Observer ” on page 408 of the “ Full Code Examples ” appendix.
In the Observer example, an observer sends updates about the state of a Task to all registered listeners in a GUI.
It's important to recognize that any Java GUI code normally uses the Observer pattern for event handling. When
you write a class that implements a listener interface like ActionListener , you are creating an observer.
Registering that listener with a component through the method addActionListener associates the observer with
an observable element, the Java GUI component.
 
Search WWH ::




Custom Search