MATLAB Object-Oriented Programming

(Joyce) #1

Overview Events and Listeners


In this section...
“Why Use Events and Listeners” on page 11-2
“Events and Listeners Basics” on page 11-2
“Events and Listeners Syntax” on page 11-3
“Create Listener” on page 11-4

Why Use Events and Listeners


Events are notices that objects broadcast in response to something that happens, such as
a property value changing or a user interaction with an application program. Listeners
execute functions when notified that the event of interest occurs. Use events to
communicate changes to objects. Listeners respond by executing the callback function.

For more information, see “Event and Listener Concepts” on page 11-14.

Events and Listeners Basics


When using events and listeners:


  • Only handle classes can define events and listeners.

  • Define event names in the events block of a class definition (“Events and Listeners
    Syntax” on page 11-22).

  • Use event attributes to specify access to the event (“Event Attributes” on page 11-
    19).

  • Call the handle handle.notify method to trigger the event. The event notification
    broadcasts the named event to all listeners registered for this event.

  • Use the handle handle.addlistener method to couple a listener to the event source
    object. MATLAB destroys the listener when the source of the event is destroyed.

  • Use the handle handle.listener method to create listeners that are not coupled to
    the lifecycle of the event source object. This approach is useful when the event source
    and the listeners are defined in different components that you want to be able to add,
    remove, or modify independently. Your application code controls the listener object
    lifecycle.


11 Events — Sending and Responding to Messages

Free download pdf