MATLAB Object-Oriented Programming

(Joyce) #1

have appropriate values for subsequently called callbacks, always create a new event data
object if you call notify with custom event data.


Manage Callback Errors


If you want to control how your program responds to errors, use a try/catch statement
in your listener callback function to handle errors.


Invoke Functions from Function Handles


When you create a function handle inside a class method, the context of the method
determines the context in which the function executes. This context gives the function
access to private and protected methods that are accessible to that class.


For example, the UpdateEvt class defines an event named Update and a listener for that
event. The listener callback is the private method evtCb.


classdef UpdateEvt < handle
events
Update
end
methods
function obj = UpdateEvt
addlistener(obj,'Update',@evtCb);
end
end
methods (Access = private)
function obj = evtCb(obj,varargin)
disp('Updated Event Triggered')
end
end
end


Private methods are normally accessible only by class methods. However, because the
function handle is created in a class method, notify can execute the callback from
outside of the class:


a = UpdateEvt;
a.notify('Update')


Updated Event Triggered


Callback Execution
Free download pdf