Best way to forward object properties

조회 수: 57 (최근 30일)
men8th
men8th . 2019년 12월 31일
댓글: men8th . 2020년 2월 12일
What is the best way to forward object properties when undertaking object oriented design with Matlab. Here I use forwarding in the sense of this Wikipedia article: Forwarding (object-oriented programming). I've asked a similar question to this before, but now I understand the terminology a bit better so I think it warrents a fresh attempt.
For example, I have a couple of objects as follows:
classdef parentObj
properties
parentProp % must stay synchronised with childThingy.childProp
childThingy childObj
end
end
classdef childObj
properties
childProp % must stay synchronised with parentProp
end
end
parentProp and childProp must always have the same value in order to maintain a consistent state.
My problem comes because there are a number of ways of achieving the forwarding and I find myself using them interchangeably and inconsistently. I'm searching for the one true way. For example, you can...
1) use an object derived from a handle class (eg a pointer) to forward properties from the parent to the child and ensure they stay synchronised
2) configure the parent object with "normal" (eg not dependent) properties and use the set method in the parent to update the corresponding properties in the child - the code analyser doesn't particularly like this and warns that the set method should not access another property to prevent problems due to initialisation order when loading saved objects
3) make the properties in the parent dependent, then use the set method to set the child properties and the get method to retrieve child properties and return them as the parent properties.
4) have a helper function in the parent, eg updateChildProperties(), which is called via the set method when the parent properties are changed.
5) use SetObservable on the parent object and add listeners on the child object. Works well apart from when the properties of the child object are dependent, and are not recalculated until accessed.
6) have a property on the child object called .parentObj. Make properties dependent on the child object, and retrieve values using the getters for the properties, reading them from .parentObj.
Any insights most gratefully received.
  댓글 수: 6
men8th
men8th 2020년 2월 12일
That's an interesting insight Adam. I've developed a similar pattern for lazy evaluation of expensive properties but without the events. The way it works is as follows:
If an object has a property which expensive to evaluate and is calculated from the values of, say, 3 input properties, I define the expensive property as a normal property (not dependent) and I also define a private property, say pIsCalculationOutOfDate. Whenever one of the 3 input properties changes is use that propertie's setter to store the changed value and then set pIsCalculationOutOfDate = true. When triggered, the setter for the expensive property calls the function to calculate the property value and returns it, the setter then stores the value. The first line of the calculation function checks to see if pIsCalculationOutOfDate = true and returns early if not, thus preventing evaluation if nothing has changed. If other expensive properties are calculated at the same time, eg as intermediate steps, these can be recorded too. It looks a bit like this:
classdef LazyEvaluationDemo < handle
properties(GetAccess = public, SetAccess = private)
expensiveProperty
end
properties
input1
input2
input3
end
properties(Private = true)
pIsCalculationOutOfDate
end
methods
function set.input1(obj,value)
obj.input1 = value;
obj.pIsCalculationOutOfDate = true;
end
function set.input2(obj,value)
obj.input1 = value;
obj.pIsCalculationOutOfDate = true;
end
function set.input3(obj,value)
obj.input1 = value;
obj.pIsCalculationOutOfDate = true;
end
function value = get.expensiveProperty1(obj)
obj.doExpensiveComputation();
value = obj.expensiveProperty1;
end
function value = get.expensiveProperty2(obj)
obj.doExpensiveComputation();
value = obj.expensiveProperty2;
end
end
methods
function value = doExpensiveCalculatoin(obj)
if ~obj.pIsCalculationOutOfDate
return
else
obj.pIsCalculationOutOfDate = false;
end
input1 = obj.input1;
input2 = obj.input2;
input3 = obj.input3;
obj.expensiveProperty1 = input1+input2+input3;
obj.expensiveProperty2 = obj.expensiveProperty1 + input1*input2/input3;
end
end
end

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by