I would like to create a singleton MATLAB class acting as a global registry. The registry should store objects (of a certain class derived from handle
) addressed with unique names. I would like to access the properties of the stored classes conveniently without temporary variables, for example:
Registry.instance().addElement('name1', NewObject(...));
Registry.instance().get('name1').Value
Registry.instance().get('name2').Value = 1;
Reading out properties of the returned class can be circumvented by removing the ()
from instance
:
>> Equipment.instance.get('name1').Value
However, it does not seem easy to use assignments because as noted in the comments, dot-indexing can't be used directly on the output of a function without assigning to an intermediate variable.
What is the proper way to implement and use such a "singleton registry" in MATLAB?
It should be noted that the singleton class contains some logic which is called when adding elements to the list, logic to properly destroy the objects in the right order and other methods which iterate through the object list. For that reason, a "normal" containers.Map
cannot be used.