I have a class with a dependent property that takes a while to be evaluated. For this reason, I would like it not to be evaluated every time it is queried, but only when the object is instantiated and when certain properties are changed. I have found this interesting solution that is based on defining an extra private property that is set whenever necessary, with the dependent property taking the value of this extra private property.
Now the problem is: how can I make sure that this private property is set when the object is instantiated and it is automatically updated when certain properties of the object are changed?
The proposed solution is perfect, just make sure that in your class constructor you define default value for both the dependent property and its proxy are set to a default value that fits your needs:
methods
function this = MyClass(...)
% default starting values for your properties
this.MyProperty1 = 0;
this.MyProperty2 = -8;
this.MyProperty3 = 3;
% calculate the initial value of your dependent
% property based on the default values of the other
% properties (this is basically the computeModulus
% function of your example)
this.UpdateMyDependent();
end
end
The logics that make your dependent property update when other properties are modified is already included in the linked thread, implement them in your class.
function this = set.MyProperty1(this,value)
this.MyProperty1 = value;
this.UpdateMyDependent();
end
function this = set.MyProperty2(this,value)
this.MyProperty2 = value;
this.UpdateMyDependent();
end
function UpdateMyDependent(this)
this.MyDependentProxy = this.MyProperty1 * this.MyProperty2;
end
function value = get.MyDependent(this)
value = this.MyDependentProxy;
end