MATLAB Object-Oriented Programming

(Joyce) #1

Event and Listener Concepts


In this section...
“The Event Model” on page 11-14
“Limitations” on page 11-15
“Default Event Data” on page 11-16
“Events Only in Handle Classes” on page 11-16
“Property-Set and Query Events” on page 11-17
“Listeners” on page 11-17

The Event Model


Events represent changes or actions that occur within objects. For example,


  • Modification of class data

  • Execution of a method

  • Querying or setting a property value

  • Destruction of an object


Basically, any activity that you can detect programmatically can generate an event and
communicate information to other objects.

MATLAB classes define a process that communicates the occurrence of events to other
objects that respond to the events. The event model works this way:


  • A handle class declares a name used to represent an event. “Name Events” on page
    11-22

  • After creating an object of the event-declaring class, attach listener to that object.
    “Control Listener Lifecycle” on page 11-28

  • A call to the handle class notify method broadcasts a notice of the event to listeners.
    The class user determines when to trigger the event. “Trigger Events” on page 11-23

  • Listeners execute a callback function when notified that the event has occurred.
    “Specifying Listener Callbacks” on page 11-30

  • You can bind listeners to the lifecycle of the object that defines the event, or limit
    listeners to the existence and scope of the listener object. “Control Listener Lifecycle”
    on page 11-28


11 Events — Sending and Responding to Messages

Free download pdf