Best way to forward object properties
조회 수: 57 (최근 30일)
표시 이전 댓글
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.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!