Bean Introspection  «Prev  Next»
Lesson 7Unicast event design patterns
Objective What are Unicast Event Design Patterns?

What are Unicast Event Design Patterns?

JavaBeans also supports unicast event sources, which are Beans that only support one event listener at any given time. If an attempt is made to register more than one listener with a unicast Bean, the add() registration method throws a TooManyListenersException exception. The design pattern for this method follows:

public void addEventListenerType(EventListenerType x) 
throws TooManyListenersException;

The only difference between this design pattern and the Design Pattern for multicast Beans is the specification of the TooManyListenersException exception .

Unicast Event Listener Registration.

Wherever possible, event sources should support multicast event notification to arbitrary collections of event listeners. However, for either semantic or implementation reasons it may sometimes be inappropriate or impractical for some events to be multicast. We therefore also permit a unicast design pattern for event registration. The type signature for a unicast EventListener registration pattern is:
public void add<ListenerType>(<ListenerType> listener)
throws java.util.TooManyListenersException;
public void remove<ListenerType>(<ListenerType> listener);

The presence of the throws java.util.TooManyListenersException in the pattern identifies the implementor as an event source with unicast event source semantics for the interface specified by <ListenerType>.

Note that the unicast pattern is a special case of the multicast pattern and as such allows migration of a unicast event source to a multicast event source, without breaking existing client code Invoking the add<ListenerType> method on a unicast source registers the specified <ListenerType> instance as the current listener only if no other listener is currently registered, otherwise the value of the currently registered listener is unchanged and the method throws:
java.util.TooManyListenersException

Passing null as the value of the listener is illegal, and may result in either an IllegalArgument-Exception or a NullPointerException.

Unicast versus Multicast

Normal event delivery is multicast. Each multicast event source is required to keep track of a set of event listeners for each different kind of event it fires. When an event is triggered, the event source shall call each eligible target listener. By default all currently registered listeners shall be considered eligible for notification, however a particular source may restrict the set of eligible listeners to a subset of those currently registered based upon an implementation dependent selection criteria that may be based upon the current state of the source, listeners, some arbitrary part of the system or the nature of the event itself. If any of the event listeners raise an exception it is an implementation decision for the event source whether to continue and deliver the event to the remaining listeners. Some event sources may only support unicast. In this case the event source is only required to keep track of a single event listener for each different unicast event it fires. When a unicast event is triggered, the event source should call this single target listener.

Event Design - Exercise

Click the Exercise link below to define event registration methods based on event design patterns.
Event Design - Exercise
In the next lesson, you find out how Beans can explicitly provide information about themselves.