Subscribe now for rapid prototyping
The Publisher-Subscriber pattern reduces object dependencies for flexible UI design
By Kurt Jacobs, JavaWorld.com, 11/09/01
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Frequent requirement changes during a software development cycle can often lead to disruption and anxiety. Software team members
scramble to update their API to accommodate new requirements. As a solution, the Publisher-Subscriber pattern can be instrumental
in reducing object dependencies; thereby reducing the need for frequent API changes. In addition, the pattern proves useful
when used for rapid prototyping. In this article, I show you how this versatile development tool can simplify your programming
tasks again and again.
Benefits of a Publisher-Subscriber implementation
You can realize the following benefits by utilizing the Publisher-Subscriber pattern:
- A publisher can publish events to 1-N subscribers. You can easily implement systems that contain more than one object that requires the same information.
- Objects communicate with each other indirectly through domain-specific events so object dependencies are reduced or eliminated.
- You can add new objects to a system with minimal impact because the API need not change. This characteristic accommodates
systems with frequent requirement changes.
- Objects have location transparency. Objects that receive events do not have to know the event's origin. Conversely, an object
can make a request via a publisher without concern for where the object carrying out the request exists.
What is the Publisher-Subscriber pattern?
The Publisher-Subscriber pattern is modeled after the publisher-subscriber relationship that exists between a consumer and
a periodical publisher. In the Publisher-Subscriber pattern there are publisher and subscriber objects. Publishers are also referred to as senders, observables, subjects, broadcasters, or notifiers. The publisher object
maintains a list of subscribers and notifies each subscriber when a state change occurs in an object of interest to that subscriber.
A publisher has one to many relationships with subscribers. Although a system may contain one or more publishers, each publisher
in all likelihood has many subscribers. For example, a system that publishes weather conditions can have several subscribers
at any one time.
Subscribers are also referred to as receivers, listeners, observers, or callbacks. A subscriber registers to receive updates
from a publisher by calling a publisher, providing a reference to itself for future callbacks and a parameter that indicates
for what object the subscriber wants updates. After registering with a publisher, the subscriber receives unsolicited updates
from a publisher whenever its monitored object experiences a state change. When you use a Publisher-Subscriber implementation
to facilitate a communication framework, the subscriber is notified each time the event type for which it has registered is
published.
The subscriber continues to receive updates until it terminates the subscription. This type of subscription is called a push subscription because data is sent to the subscriber without the client requesting each delivery. Each subscriber may want to monitor more
than one object or receive more than one event type notification. For example, an online trading application may contain an
object that subscribes to several different market data event types for a particular stock symbol. This online trading object
may subscribe to events pertaining to the volume transacted for a specific stock symbol or to events relating to each trade
for that symbol during any given day. At any time, a subscriber may unsubscribe to all updates without affecting other objects
in the system.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- Download the source code for this article. The pubsub.jar demonstrates the use of the Publisher-Subscriber pattern. Readers
can create their own executable from the source code if they desire
http://www.javaworld.com/javaworld/jw-11-2001/subscriber/jw-1109-subsource.zip
- For more information on design patterns, read Design PatternsElements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (Addison-Wesley, January 1995)
http://www.amazon.com/exec/obidos/ASIN/0201633612/javaworld/
- For more information about implementing the Publisher-Subscriber pattern, visit
http://www.dralasoft.com/products/eventbroker/paper/
- For a list of more interaction patterns
http://www.icis.qut.edu.au/~steve/OO/book/Strpat00000085.html
- "Dispatcher Eases Workflow Implementation," Michael Ball (JavaWorld, October 2001)
http://www.javaworld.com/javaworld/jw-10-2001/jw-1019-dispatcher.html?
- "Pattern Your Way to Automated Regression Testing," Kevin Pauli (JavaWorld, September 2001)
http://www.javaworld.com/javaworld/jw-09-2001/jw-0921-test.html?
- Don't miss JavaWorld's new Java Design Patterns column by David Geary
http://www.javaworld.com/columns/jw-java-design-patterns-index.shtml
- For more articles on Design Patterns, visit the JavaWorld Topical Index
http://www.javaworld.com/channel_content/jw-patterns-index.shtml
- Subscribe to JavaWorld's free weekly email newsletters
http://www.idg.net/jw-subscribe
- Speak out in our Java Forum
http://forums.idg.net/webx?13@@.ee6b802
- You'll find a wealth of IT-related articles from our sister publications at IDG.net