Local database reading edition
Observer Pattern - How to Propagate Changes?
Reconstructed from database records, not a captured original forum layout. Dates and display names are retained as recorded; timezone and real-world identity are not inferred. Historical advice, contact details and offers are not current. Original markup is shown as inert text.
Forum index · Object Oriented Design
Opening post
Padma · 2001-04-20 20:03:50
Observer Pattern
Suppose you want to propagate some changes to client. How can you do it?
There is a pattern we can apply for that. This is the observer pattern.
This define a one-to many dependency between objects so that when one-object changes state, all its dependents are notified and updated automatically.
How can we do it?
Define a Subject class that maintains a list of Observers that are interested in changes in the subject. Whenever the subject changes, sends an update message to each of the observers of that subject.
Example:
Lets take a system that is required to configure. This can be done using property files.
Suppose we have a component that loads the configuration from properties files and rest of the client components (of the configuration component) get the properties from the configuration components. That is fine.
Next, there is a requirement to change the configuration on the fly. Then we have to find out a way to accomplish this. Apply observer pattern.
Forcus should be to
· develop the objects separately, to minimize coupling (what they know about eachother) and increase reuse (using one without the other).
Solution
Provide an interface
· subscribing to receiving notifications.
· unsubscribing to stop receiving notifications.
· querying the configuration, so that observers can get the information they need.
· receiving the notifications from the subject.
Example
Following methods should implement in ConfigComponent. All the subscribers need to hold by the ConfigComponent. Remove them when they unsubscribe.
ConfigComponent.java
// Clients register to receive notifications.
subscribe(ConfigListenerInterface o)
{ }
// Clients unregistere to stop receiving notifications.
unsubscribe(ConfigListenerInterface o)
{ }
//Clients make Quires
query(String property)
{ }
// Config components
notify()
{}
//ConfigComponent refresh the configuration
refresh()
{
// update configuration
notify();
}
ConfigListenerInterface.java
Update();
All the clients who need to receive the notification required subscribe to ConfigComponent. Those clients need to implements the ConfigListenerInterface
Consequences
· Low coupling because the subject does not need to know exactly what information the observers need.
· The number of observers per subject and the number of subjects per observer can vary at run-time.
· Efficiency may suffer because of spurious notifications.
When notifying, there may be a thing to consider. For example, it may need to notify only if there is an effect from change to the client. May be need to get the lock for the object before notifying, if change may cause a problem if it happens when client component in the middle some process. So, there may other condition to think depending on the requirement of the notification.
Padma
Source notes
MyCommunity/nandighosha.org.zip!/forum/tools/snitz_forums_2000.mdb
Table: FORUM_TOPICS + FORUM_REPLY · record IDs: 110, 26, 31