MATLAB Object-Oriented Programming

(Joyce) #1

When to Use AbortSet


Use of the AbortSet attribute does incur some overhead in the comparison of the
current and new property values. Using the AbortSet attribute can slow all property
assignments because the current and assigned value are always compared before the
assignment is made. The AbortSet attribute is most useful when:



  • You want to prevent notification of the PreSet and PostSet events and execution of
    the listener callbacks when the property value does not change.

  • The cost of setting a property value is greater than the cost of comparing the current
    property value with the value being assigned, and you are willing to incur the
    comparison cost with all assignments to the property.


Implement AbortSet


The following example shows how the AbortSet attribute works. The AbortTheSet
class defines a property, PropOne, that has listeners for the PreGet, PreSet, PostGet,
and PostSet events and enables the AbortSet attribute.


NoteTo use this class, save the AbortTheSet class in a file with the same name in a
folder on your MATLAB path.


classdef AbortTheSet < handle
properties (SetObservable, GetObservable, AbortSet)
PropOne = 7
end
methods
function obj = AbortTheSet
addlistener(obj,'PropOne','PreGet',@obj.getPrePropEvt);
addlistener(obj,'PropOne','PreSet',@obj.setPrePropEvt);
addlistener(obj,'PropOne','PostGet',@obj.getPostPropEvt);
addlistener(obj,'PropOne','PostSet',@obj.setPostPropEvt);
end
function propval = get.PropOne(obj)
disp('get.PropOne called')
propval = obj.PropOne;
end
function set.PropOne(obj,val)
disp('set.PropOne called')
obj.PropOne = val;


Assignment When Property Value Is Unchanged 11-

Free download pdf