Best way to forward object properties
7 views (last 30 days)
Show older comments
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 Comments
Adam
on 12 Feb 2020
Edited: Adam
on 12 Feb 2020
I did create a class that acted as a kind of half-way house between a regular property and a dependent property, called a JustInTimeProperty to handle situations like this, although I always forget how to use my own class as obviously it can't behave as an actual property attribute like 'Dependent', it is a property itself.
In essence though, what it does is wrap up a private property with a paired dependent property and a 'calculation function'.
So the get function for the dependent property will check if the private property is empty. If it isn't empty it will just return it, if it is empty it will call the calculation function to calculate and set the private property.
I also pass in an object of another class I wrote, let's call it EventHub, whose role is simply to allow dependencies to be registered in the form of an object and events (or 'PostSet' property events). Whenever any of those registered events trigger this class fires off its own event (so basically it collects any number of disparate events and funnels them all into a single new event). The point of this is that the JustInTimeProperty listens for this one event and responds by setting the private stored property back to empty and firing off its own event. It also abstracts away those other dependencies into this EventHub rather than in the JustInTimeProperty itself, though they could also have been registered with it directly for the same effect I guess.
So, it is rather complicated (though I already had that second class for other purposes so was easier for me to design the JustInTimeProperty to use it rather than implement its own dependency handling), but it does its job - the idea being that if I have something that takes a long time to calculate I register its calculation function with this class, alongside an object of the EventHub class. I register any events that would cause the value to need to be recalculated in the EventHub, e.g. 'PostSet' events for parameter properties changing. Then if one of those events triggers, the JustInTimeProperty resets, fires off its event so that anything that was using it knows it has changed and can request it again if they want. Then when they make that request, because the private property is now empty it will trigger the calculation function again.
I chose not to go down the route of having the JustInTimeProperty automatically call the calculation function again on getting notification of a dependency changing because it may be it no longer needs to be recalculated so it is wasted - hence it sending an event so the function/class using it can decide if it wants to trigger the update.
It would be nice though if such a property type were embedded in the Matlab language as this isn't a trivial implementation, even though the two classes are only 90 and 58 lines, including lots of white space and method/property blocks! It works for what I wanted though.
Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!