In Matlab class it seems to be syntactically correct to declare property that is Dependent (computed not stored) and Observable in the same time. Consider code
properties (Access = private)
instanceOfAnotherClass
end
properties (SetAccess = private, Dependent, SetObservable)
propertyTwo
end
methods
function val = get.propertyTwo(this)
val = this.instanceOfAnotherClass.propertyOne;
end
end
Does this work as expected? That is, if the property propertyOne of an object stored in instanceOfAnotherClass is changed is there property change event triggered by propertyTwo? Note that propertyOne is not Observable.
Edit:
It does not work (as I expected). ‘PostSet’ event is not triggered. So how do I deal with this kind of situation? Is there a better solution then to create propertyTwo as a non Dependent and set it to the same value as ‘propertyOne’ every time ‘propertyOne’ changes?
Edit2:
In reaction to Amro’s edit of his answer I will explain situation more complex.
Consider this 2 classes:
classdef AClass < handle
properties
a
end
end
classdef BClass < handle
properties (Access = private)
aClassInst
end
properties (Dependent, SetObservable, SetAccess = private)
b
end
methods
function this = BClass(aClass)
this.aClassInst = aClass;
end
function val = get.b(this)
val = this.aClassInst.a;
end
end
end
The class that uses all this code should not get access to AClass. It interacts only with instance of BClass and wants to listen to changes of property b. however if I make property a of AClass observable that would not solve my problem, would it? The ‘PostSet’ events are not going to propagate to property b, are they?
It might be syntactically correct, but the listener callback will never execute. Example:
Now try:
As you can see the ‘PostSet’ callback is never executed.
EDIT
The way I see it,
SetObservableshould really be set onanotb. Its becausebis read-only and can only change ifachanges. Now thePostSetevent would notify us that both properties have changed.Use the same example I used above, simply move
SetObservablefrombtoa. Of course now you listen to the event as:EDIT#2
Sorry I didn’t pay attention to the fact that you have composition (BClass has an instance of AClass as private property).
Consider this possible solution:
AClass.m
BClass.m
Basically we set property
aof AClass as observable.Next inside the constructor of BClass, we register a listener for the AClass instance passed to listen on property
achanges. In the callback we notify listeners of this object thatbhas changed as wellSince we can’t really raise a
PostSetmanually, I created a custom eventbPostSetwhich we raise in the previous callback function. You can always customize the event data passed, refer to the documentation to see how.Here is a test case:
The output was:
Notice how we only interact with the BClass instance when we register our listener. Of course since all classes derive from
handleclass, the instanceaand the private propertyaClassInstboth refer to the same object. So any changes toa.aare immediately reflected onb.aClassInst.a, this causes the internalaPostSet_EventHandlerto execute, which in turn notify all registered listeners to our custom event.