在 Matlab 中使用事件侦听器而不是函数回调的好处
Benefit of using event listeners over functions callbacks in Matlab
注意:这里的问题是针对Matlab的情况!
这个问题的上下文是一个 Matlab 句柄 class,它通过 websocket 接收一些数据,对该数据进行一些处理,然后将数据转发给其他一些 classes,这用回调注册自己。
此 class 的简化版本如下:
classdef ReceiverWebsocket < WebSocketClient
properties (Access = private)
dataReceivers_ % Stores the callback functions and their desired rate
end
methods
function obj = ROSReceiverWebsocket(TOPIC, URI, varargin)
obj = obj@WebSocketClient(URI, varargin{:});
obj.dataReceivers_ = containers.Map;
end
function registerReceiver(obj, name, rate, callback)
cb.name = name;
cb.callback = callback;
obj.dataReceivers_(name) = cb;
end
% Implement the abstract callback functions
function onTextMessage(obj,message)
data = jsondecode(message);
for key = keys(obj.dataReceivers_)
cb = obj.dataReceivers_(cell2mat(key));
cb.callback(data);
end
end
end
end
在研究如何在 Matlab 中实现这样的功能时,我也遇到了 event listeners and notifyers in Matlab 的概念。
据我了解,可以使用 handle-class 事件重新实现上述功能,然后在 onTextMessage(...)
函数中调用 notify(...)
而不是调用直接回调函数。
我现在的问题是,如果有:
- 使用事件侦听器功能比直接回调有什么好处?
- 使用直接回调有什么缺点吗?
尤其是我无法找到有关通知事件回调如何执行的任何信息,关于执行顺序、任何执行延迟甚至不同线程中的执行。
一如既往,这取决于您的用例。
就我个人而言,我认为使用事件的主要优点是您可以通过单个事件触发多个侦听器(无需额外编码,请参见此处的示例:https://mathworks.com/matlabcentral/answers/13305-can-timers-trigger-a-single-event-that-many-objects-listen-to)。
它还允许您使用可能不同的参数列表进行回调,而在您的示例中,回调函数必须只有一个参数,即数据。
关于执行顺序(https://de.mathworks.com/help/matlab/matlab_oop/callback-execution.html#bu0b2f4):
The order in which listeners callback functions execute after the firing of an event is undefined. However, all listener callbacks execute synchronously with the event firing.
注意:这里的问题是针对Matlab的情况!
这个问题的上下文是一个 Matlab 句柄 class,它通过 websocket 接收一些数据,对该数据进行一些处理,然后将数据转发给其他一些 classes,这用回调注册自己。
此 class 的简化版本如下:
classdef ReceiverWebsocket < WebSocketClient
properties (Access = private)
dataReceivers_ % Stores the callback functions and their desired rate
end
methods
function obj = ROSReceiverWebsocket(TOPIC, URI, varargin)
obj = obj@WebSocketClient(URI, varargin{:});
obj.dataReceivers_ = containers.Map;
end
function registerReceiver(obj, name, rate, callback)
cb.name = name;
cb.callback = callback;
obj.dataReceivers_(name) = cb;
end
% Implement the abstract callback functions
function onTextMessage(obj,message)
data = jsondecode(message);
for key = keys(obj.dataReceivers_)
cb = obj.dataReceivers_(cell2mat(key));
cb.callback(data);
end
end
end
end
在研究如何在 Matlab 中实现这样的功能时,我也遇到了 event listeners and notifyers in Matlab 的概念。
据我了解,可以使用 handle-class 事件重新实现上述功能,然后在 onTextMessage(...)
函数中调用 notify(...)
而不是调用直接回调函数。
我现在的问题是,如果有:
- 使用事件侦听器功能比直接回调有什么好处?
- 使用直接回调有什么缺点吗?
尤其是我无法找到有关通知事件回调如何执行的任何信息,关于执行顺序、任何执行延迟甚至不同线程中的执行。
一如既往,这取决于您的用例。
就我个人而言,我认为使用事件的主要优点是您可以通过单个事件触发多个侦听器(无需额外编码,请参见此处的示例:https://mathworks.com/matlabcentral/answers/13305-can-timers-trigger-a-single-event-that-many-objects-listen-to)。
它还允许您使用可能不同的参数列表进行回调,而在您的示例中,回调函数必须只有一个参数,即数据。
关于执行顺序(https://de.mathworks.com/help/matlab/matlab_oop/callback-execution.html#bu0b2f4):
The order in which listeners callback functions execute after the firing of an event is undefined. However, all listener callbacks execute synchronously with the event firing.