I am having multiples classes which needs to read a shared common variable called 'MasterData'.
This MasterData variable is getting its data from an api call. In order to avoid to make this api call in each of my different classes that need it, I wanted to define MasterData as a propery in a handle class (MasterDataConfiguration class) and then make other classes inherit from that superclass.
classdef MasterDataConfiguration < handle
properties
MasterData
end
methods
function obj = MasterDataConfiguration2(~)
ini = IniConfig();
ini.ReadFile('MPS_AutoJSON_ConfigFile.ini');
APIbaseurl = ini.GetValues('Web App API', 'PreformFilesrequestURI_Prod');
APIparameter = 'admin/masterdata';
options = weboptions('Timeout',120);
obj.MasterData = webread([APIbaseurl,APIparameter],options);
disp('MasterData API was queried');
end
For the sub clases which would inherit from the above class:
classdef SubClassExample < MasterDataConfiguration
properties
ColdHalfDataStruct
end
methods
function obj = SubClassExample(OtherObj)
obj.MasterData
end
end
end
My difficulty here is that I get the API requested each time I create a subclass object SubClassExample.
What I would like is that the attribute MasterData from class MasterDataConfiguration gets set once and for all and the other subclasses simply refers to it without re generating it.
I tried using Properties (constant) but I can't make an API call in such way.
I read some documentation here :
But could not understand whet is meant by the following :
"MATLAB® evaluates property default values only once when loading the class. MATLAB does not reevaluate the assignment each time you create an object of that class. If you assign an object as a default property value in the class definition, MATLAB calls the constructor for that object only once when loading the class."
In my case I see the API is always called.
Thanks in advance for any hint on how to proceed.
0 Comments
Sign in to comment.