Lesson 7 | Unicast 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.
Quiz Question on JavaBeans
Which of the given statements are true, if a JavaBean has the string property name, on which reading and writing are allowed? (Choose two)
- The getter and setter methods must be public.
- For boolean properties, the setter method must always start with "is".
- The setter method must have the property type as the return type.
- The setter method must have the property type as the argument type.
- The getter method must have the property type as the argument type.
Answer: A, D
Choices A and D are the correct answers.
According to the JavaBeans naming standards, if the property name is 'x' and the type is Type, the accessor method is of the form:
public Type getX()
and the mutator method is of the form:
public void setX(Type newValue)
However, boolean property uses another convention as the following as well:
public boolean isX()
public void setX(boolean newValue)
For read/write properties, both getter and setter methods must be public, so choice A is correct.
Properties of boolean type may use getter methods starting with "get" or "is".
So choice B is incorrect. The setter method must have the property type as the argument type, while the getter method must have the property type as the return type.
Hence, choice D is correct, while choices C and E are incorrect.
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.