org.gjt.universe.notification
Class NotificationCenter

java.lang.Object
  |
  +--org.gjt.universe.notification.NotificationCenter

public class NotificationCenter
extends java.lang.Object

The instances of this class implement a detached rendezvous system between objects which post notifications and objects which are interested in them. This approach has the following benefits over a scheme where notification events are passed directly between the poster and the observer: - each notification generating object need not manage the list of observers itself, thus eliminating duplication of code when it is a subclass of a library class without this support. - observers can register for notifications of a certain type, notifications from a certain sender, or a combination as desired. - observers need not know about specific instances of senders, merely the types of notifications that might be sent. - weak references are utilized to ensure that observers do not reside permanently in memory simply because they are still registered as a observer. - observers can block as long as they wish while processing a notification without affecting the handling of GUI events. Most code will simply use the single default instance of this class provided by the defaultCenter class method. However, applications which have a large number of notifications which can be logically segmented, may wish to create individual NotificationCenters known to the relevant parties in order to improve dispatch performance. EXAMPLE OF REGISTERING NotificationCenter aCenter = NotirficationCenter.defaultCenter(); aCenter.addObserver( obs1, "On Fire" ) - obs1 wants its handleNotification() method called whenever anything posts an "On Fire" notification. aCenter.addObserver( obs2, "On Fire", myHouse ) - obs2 only cares about when my house is on fire. aCenter.addObserver( obs3, myHouse ) - obs3 wants its handleNotification() method called whenever anything happens to the my house.

Version:
$Id: NotificationCenter.java,v 1.3 2001/05/13 17:08:08 noordvyk Exp $
Author:
Allan Noordvyk (noordvyk@home.com)
See Also:
Notification

Field Summary
private  java.util.Hashtable myByBothTable
           
private  java.util.Hashtable myByNameTable
           
private  java.util.Hashtable myBySenderTable
           
private static NotificationCenter ourDefaultCenter
           
 
Constructor Summary
NotificationCenter()
          This is the designated constructor which should be used to create new instances of this class.
 
Method Summary
 void addObserver(NotificationObserver anObserver, java.lang.Object aSender)
          This method adds the given object as an observer for events posted by the given sending object regardless of the notification name name.
 void addObserver(NotificationObserver anObserver, java.lang.String notificationName)
          This method adds the given object as an observer for events posted by any object, but having the given notification name.
 void addObserver(NotificationObserver anObserver, java.lang.String notificationName, java.lang.Object aSender)
          This method adds the given object as an observer for events posted by the given sending object and having the given name.
static NotificationCenter defaultCenter()
          This method returns the default notification center for the application, creating it if necessary.
 void postNotification(Notification aNotification)
          This method posts the given notification through the notification center.
 void removeObserver(NotificationObserver anObserver, java.lang.Object aSender)
          This method removes the given object as an observer for events posted by the given sending object regardless of the notification name name.
 void removeObserver(NotificationObserver anObserver, java.lang.String notificationName)
          This method removes the given object as an observer for events posted by any object, but having the given notification name.
 void removeObserver(NotificationObserver anObserver, java.lang.String notificationName, java.lang.Object aSender)
          This method removes the given object as an observer for events posted by the given sending object and having the given name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ourDefaultCenter

private static NotificationCenter ourDefaultCenter

myByNameTable

private java.util.Hashtable myByNameTable

myBySenderTable

private java.util.Hashtable myBySenderTable

myByBothTable

private java.util.Hashtable myByBothTable
Constructor Detail

NotificationCenter

public NotificationCenter()
This is the designated constructor which should be used to create new instances of this class.

Method Detail

defaultCenter

public static NotificationCenter defaultCenter()
This method returns the default notification center for the application, creating it if necessary.


addObserver

public void addObserver(NotificationObserver anObserver,
                        java.lang.String notificationName,
                        java.lang.Object aSender)
This method adds the given object as an observer for events posted by the given sending object and having the given name. If the observer has already registered for notification using this method, no change will be made (i.e. the observer will only be notified once). Note that the notification center maintains only a weak reference to the observer and the sender, and thus either may be garbage collected if no other references to it exist.

Parameters:
anObserver - The object that wishes to be notified.
notificationName - The name of the notification expected to be posted.
aSender - The object expected to post the notification.

addObserver

public void addObserver(NotificationObserver anObserver,
                        java.lang.String notificationName)
This method adds the given object as an observer for events posted by any object, but having the given notification name. If the observer has already registered for notification using this method, no change will be made (i.e. the observer will only be notified once). Note that the notification center maintains only a weak reference to the observer, and thus the observer may be garbage collected whlie registered if no other references to it exist.

Parameters:
anObserver - The object that wishes to be notified.
notificationName - The name of the notification expected to be posted.

addObserver

public void addObserver(NotificationObserver anObserver,
                        java.lang.Object aSender)
This method adds the given object as an observer for events posted by the given sending object regardless of the notification name name. If the observer has already registered for notification using this method, no change will be made (i.e. the observer will only be notified once). Note that the notification center maintains only a weak reference to the observer and the sender, and thus either may be garbage collected if no other references to it exist.

Parameters:
anObserver - The object that wishes to be notified.
aSender - The object expected to post the notification.

removeObserver

public void removeObserver(NotificationObserver anObserver,
                           java.lang.String notificationName,
                           java.lang.Object aSender)
This method removes the given object as an observer for events posted by the given sending object and having the given name. If the observer is not currently registered for these notifications, this method does nothing.

Parameters:
anObserver - The object that wishes to resign from being be notified.
notificationName - The name of the notifications which were to be detected.
aSender - The object being observed.
See Also:
addObserver

removeObserver

public void removeObserver(NotificationObserver anObserver,
                           java.lang.String notificationName)
This method removes the given object as an observer for events posted by any object, but having the given notification name. If the observer did not register for such notifications this method does nothing.

Parameters:
anObserver - The object that wishes to resign from being be notified.
notificationName - The name of the notification that was being observerd.
See Also:
addObserver

removeObserver

public void removeObserver(NotificationObserver anObserver,
                           java.lang.Object aSender)
This method removes the given object as an observer for events posted by the given sending object regardless of the notification name name. If the observer has already registered for notification using this method, no change will be made (i.e. the observer will only be notified once).

Parameters:
anObserver - The object that wishes to resign from being be notified.
aSender - The object that was being observed.

postNotification

public void postNotification(Notification aNotification)
This method posts the given notification through the notification center. Any observers which have registered an interest in the notification by name, sender, or both will be notified. Note that each observer will only get a single notification even if they have registered separately for notification based on each of name and sender.

Parameters:
aNotification - The notification to be posted.


Copyright © 2001 Universe Dev Team All Rights Reserved.