Matlab proxy class problem. (re: dependent properties)

Consider the following Matlab classes (2009a):

classdef BigDeal < handle properties hugeness = magic(2000); end methods function self = BigDeal() end end end classdef BigProxy < handle properties(Dependent) hugeness end properties(Access = private) bigDeal end methods function self = BigProxy(bigDeal) self.bigDeal = bigDeal; end function value = get.hugeness(self) value = self.bigDeal.hugeness; end end end 

Now consider the following uses:

Setup:

 >> b = BigDeal b = BigDeal handle Properties: hugeness: [10x10 double] Methods, Events, Superclasses 

OneLayer:

 >> pb = BigProxy(b) pb = BigProxy handle Properties: hugeness: [10x10 double] Methods, Events, Superclasses 

TwoLayers:

 >> ppb = BigProxy(pb) ppb = BigProxy handle with no properties. Methods, Events, Superclasses 

Question: Why can't my two-layer proxy see hugeness when there can be one layer? Dependent properties can be calculated - but for some reason this happens only on one layer for some reason?


Update: See my answer below for a workaround.

+4
source share
2 answers

The problem here is twofold:

  • The constructor of a BigProxy object BigProxy designed to accept an input object that has a (independent) hugeness property (for example, a BigDeal object), which can have a BigProxy object calculate the value for its own dependent hugeness property.

  • You pass the BigProxy object to the BigProxy constructor BigProxy you create ppb , and you apparently cannot calculate the dependent property from another dependent property. For example, this is an error that occurs when trying to access ppb.hugeness :

     ??? In class 'BigProxy', the get method for Dependent property 'hugeness' attempts to access the stored property value. Dependent properties don't store a value and can't be accessed from their get method. 

    In other words, the BigProxy external object BigProxy trying to calculate the value of its dependent hugeness property by accessing the stored hugeness value for the internal BigProxy object, but there is no stored value because it is a dependent property .

I think that a fix for such a situation would be for the BigProxy constructor BigProxy check the type of the input argument to make sure it is a BigDeal object, and otherwise make a mistake.

+4
source
Gnowice gave a good answer to the question “why,” so I awarded him a green glory test. However, for those who want to work, you can do something like this:
 classdef BigDeal < handle properties hugeness = magic(10000); end methods function self = BigDeal() end end end classdef BigProxy < handle properties(Dependent) hugeness end properties(Access = private) bigDeal end methods function self = BigProxy(bigDeal) self.bigDeal = bigDeal; end function value = get.hugeness(self) value = self.getHugeness; end end methods(Access = private) function value = getHugeness(self) if isa(self.bigDeal,'BigProxy') value = self.bigDeal.getHugeness; else value = self.bigDeal.hugeness; end end end end 

which will allow you to do the following

 >> b = BigDeal; >> pb = BigProxy(b); >> ppb = BigProxy(pb); 

And each of { b , pb , ppb } will have the same public methods and properties. The only drawback is that I had to (without IMHO need) clutter BigProxy with a new personal recipient.

+4
source

Source: https://habr.com/ru/post/1342057/


All Articles