Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Programming Java threads in the real world, Part 6

The Observer pattern and mysteries of the AWTEventMulticaster

  • Print
  • Feedback

Page 3 of 6

Listing 2 (/src/com/holub/tools/Subscriber.java): A simple Observer


01  
02  
package com.holub.tools;
/**
  |      The interface implemented by subscribers to a multicaster. In the multicaster model, a publisher can also be a subscriber, so the Subscriber interface extends EventListener to give you some flexibility. This way the top-level publisher can choose to be a Subscriber (that is, implement Subscriber) or to use a Subscriber (that is, contain a reference to a Multicaster, which is a Subscriber).
@see Multicaster
 */
03  
04  
05  
    /**
      |      The receive message is sent by the publisher (which either "is" or "uses" a multicaster) to all subscribers.
@param publication An object sent to the subscribers to give them information about what happened.
     */
06  
07  
08  


Listing 3 shows a publisher implementation that corresponds to the subscriber in Listing 2. It provides methods that let you add a new subscriber, remove a subscriber, and publish news of an event to the subscribers. The publish() method (Listing 3, line 16) just publishes the string ("Hello world") to the subscribers.

Note that the list is copied by converting it to an array on line 20 of Listing 2. Unfortunately, you can't use clone() to copy a generic Collection. Of the two things you can do (make an empty list and explicitly copy the Collection into it or convert the Collection to an array), array conversion seems the most appropriate in the current code: an array is both smaller and easier to assemble than another LinkedList.

I'll come back to this listing in a moment.

Listing 3 (Publisher.java): A simplistic notifier


01  
02  
03  
04  
05  
06  
07  
08  
09  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
import java.util.*;
import com.holub.tools.Subscriber;
public class Publisher
{
    Collection subscription_list = new LinkedList();
   synchronized public void subscribe( Subscriber subscriber )
    {   subscription_list.add( subscriber );
    }
   synchronized public void cancel_subscription( Subscriber subscriber )
    {   subscription_list.remove( subscriber );
    }
    //------------------------------------------------------------------
   public void publish( )          // usually called by other publisher
    {                               // methods so would be private
        Object[] copy;  
        synchronized( this )
       {   copy = subscription_list.toArray();
        }
        for( int i = 0; i < copy.length; ++i )
            ((Subscriber) copy[i]).receive("Hello world");
    }
    //------------------------------------------------------------------
   public synchronized void publish_blocking()
    {
        for(Iterator i = subscription_list.iterator(); i.hasNext();)
            ((Subscriber) i.next()).receive("Hello world");
    }
    //...
}


The Hello_world class in Listing 4 shows how the publisher and subscriber classes work together. main() manufactures a Publisher on line 5 and a Subscriber on line 6. The Subscriber implementation is an anonymous inner class whose receive() override prints the String that's passed in from the publisher as the publication argument ("Hello world"). In main(), the subscriber is forced to subscribe to the publication (this is not a democracy), and then the publisher is told to publish the event.

  • Print
  • Feedback

Resources