Subscribe now for rapid prototyping

The Publisher-Subscriber pattern reduces object dependencies for flexible UI design

1 2 Page 2
Page 2 of 2

Java's Observable class, also found in Java's util package, is a publisher. The Observable class maintains a list of Observers and notifies each Observer when changes occur in the Observable object. You can create a Publisher class to service a system by establishing a collection of Observable objects. Each Observable object in turn maintains a list of Observers. Your system's specific needs will determine the Publisher's implementation.

You can create a Publisher by utilizing Java's Observable and Hashmap classes. Each subscriber passes to the publisher a reference to itself for callbacks and a string to indicate for which event type it wants updates. A string can represent each event type used in your system. These event type strings are passed to a publisher and used as a key to an Observable instance. The Observable instance in turn maintains a list of all the subscribers interested in receiving the events of the key's type.

An alternative implementation for the subscribe() method uses an object instance to specify the object of interest rather than an object identifier (such as a String). Subscribers subscribe to the event of interest by passing the subscribe() method an instance of the object that the subscriber wishes to monitor.

The following table illustrates a Publisher implementation. The Publisher in this instance associates 1-N Observers or Subscribers with a particular event type. The event types are identified by a String.

Publisher event types/Observable mapping
Event typeObservable's list of Observers
Status Event

Status Subscriber #1

Status Subscriber #2

Status Subscriber #3

Data Request EventData Request Subscriber #1
Data Response Event

Data Response Subscriber #1

Data Response Subscriber #2

The following code shows a subscribe() method implementation within a Publisher object for an event notification system. Each object in the system subscribes to events by passing a reference to an Observer (the object that will process the event when it is published) and an identifier that indicates what type of event the object wants to receive.

    public void subscribe(Observer eventSubscriber, String eventTypeHashKey)
    {
        // Simple class that extends Observable
        EventObservable eventType = null;
        // If there already exists an Observable object that has a list
        // of subscribers subscribing to this particular event
        // type, and then add the new subscriber (eventSubscriber) to the list.
        if (mEventTypes.containsKey(eventTypeHashKey))
        {
            eventType = (EventObservable)mEventTypes.get(eventTypeHashKey);
            eventType.addObserver(eventSubscriber);
        }
        // This is the first subscriber to subscribe to an event of this
        // event type.  Create a new Observable instance and add the
        // new subscriber (eventSubscriber) to the list.
        else
        {
            eventType = new EventObservable();
            eventType.addObserver(eventSubscriber);
            mEventTypes.put(eventTypeHashKey, eventType);
        }
    }
    /**
     * This method is called by a subscriber when the subscriber no longer
     * wants to be notified about events of a specified type.
     */
    public void unsubscribe(Observer eventSubscriber, String eventTypeHashKey)
    {
        Observable eventType = null;
        if (mEventTypes.containsKey(eventTypeHashKey))
        {
            eventType = (EventObservable)mEventTypes.get(eventTypeHashKey);
            eventType.deleteObserver(eventSubscriber);
        }
    }
    /**
     * This publish() method is called to publish an event to 1 to N subscribers.
     */
    public void publish(Event reservationEvent)
    {
        String eventTypeHashKey = null;
        EventObservable eventType = null;
        eventTypeHashKey = reservationEvent.getEventType();
        eventType = (EventObservable)mEventTypes.get(eventTypeHashKey);
        eventType.notifySubscribers(reservationEvent);
    }

Subscribing to the pattern

The Publisher-Subscriber pattern provides a flexible framework for a software system. It accommodates frequent requirement changes by reducing object dependencies. The communication mechanism provides location transparency, making it well suited for use in distributed systems. A publisher object's ability to publish information to multiple subscribers makes this pattern a good choice for rapid prototyping as well. Every programmer should become familiar with the Publisher-Subscriber pattern for development ease.

Learn more about this topic

1 2 Page 2
Page 2 of 2