|
|
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
Page 3 of 6
|
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.
|
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.