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

Reply 26

Arun Patnaik · 2001-04-23 13:07:46

Just a note of clarification. All this works for single VM cases. If the system consists of multiple VMs think about using JMS along with Message Beans(sotr of a distributed Observer Pattern). Thanks - Arun Arun Patnaik Staff S/w Engineer II Sybase Inc

Reply 31

administrator · 2001-04-25 10:48:57

<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote> All this works for single VM cases. If the system consists of multiple VMs think about using JMS along with Message Beans(sort of a distributed Observer Pattern). <hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote> Regardless of number of VMs Observer Pattern works in all cases. My understanding is regardless of what technology one will use, if the observer can able to notify all its dependents by any means (e.g. RMI, JMS); Observer pattern can still be applied in a distributed environment. The location of the object should be abstracted and depending on the platform, developers can choose a suitable way of communication if objects are distributed. Ahimanikya Satapathy http://www.ammu.org/kuku

Source notes

MyCommunity/nandighosha.org.zip!/forum/tools/snitz_forums_2000.mdb
Table: FORUM_TOPICS + FORUM_REPLY · record IDs: 110, 26, 31