Table 15.1 gives a review of the major roles in a generic event model. Figure 15.1 shows them in a schematic way.
Role | Description |
---|---|
Event source | The object which potentially causes an event to happen – an event can be any special occurrence. The event source provides a way for interested event consumers to register their interest with this event source. The event source typically keeps a list of registered event consumers, so that when the event occurs the registered consumers in the list are notified. |
Event consumer | The object which is interested in listening to a particular event.
An event consumer contains a special method called the event handler. This method takes in the event object as a parameter. Event sources and event consumers can have a many-to-many relationship – one event consumer can register with many event sources, and one event source can have multiple event consumers registered for the same event. |
Event object | When an event occurs in the event source, a new event object is created. This event object is then passed over to the event consumer's event handler method as a parameter. An event object encapsulates event-specific information (e.g. the label of the button clicked) which the event consumer can extract. |
Let us examine the sequence of actions in the generic event model.
1. | The event consumer registers its interest with an event source and provides the name of the event handler that should be invoked when the event occurs. |
2. | The event source keeps a reference to each registered event consumer in an internal list. |
3. | When the event occurs, the event source creates a new instance of an event object and passes into it event-specific information. |
4. | The event source goes down the list of registered event consumers and invokes each event handler, passing in the event object as a parameter. |
5. | The event handler of the event consumer runs. Event information can be obtained from the event object passed in. |
Figure 15.2 depicts this sequence.
18.191.215.96