How to add set/get methods methods in class constructor

11 visualizaciones (últimos 30 días)
Collin Smith
Collin Smith el 6 de Abr. de 2021
Respondida: Steven Lord el 6 de Abr. de 2021
I have a class with some properties that are linked to a registry. I would like the class to automatically retrieve values from the registry anytime one of those properties is accessed; conversely, I would like to push those values to a registry anytime a value is modified by the program. Rather than define each set/get method individually, I would like to set these set/get methods during the class constuctor. Some pseduocode is defined below:
classdef ExampleClass < handle
properties(SetObservable,GetObservable)
prop1;
prop2;
prop3; %etc.
end
methods
function obj = ExampleClass(varargin)
obj.registry = Registry(); %Defined elsewhere
proplist = fields(obj);
for ii = 1:length(proplist), SomeFuntionToAddSetMethod(obj,proplist{ii}, @obj.ListenerPullValues); end
for ii = 1:length(proplist), SomeFuntionToAddGetMethod(obj,proplist{ii}, @obj.ListenerPushValues); end
end
end
methods(Access = private)
function ListenerPushValues(obj,eventData,~)
obj.registry.WriteValue(eventData.Name,obj.(eventData.Name)); %write value in registry
end
function ListenerPullValues(obj,eventData,~)
obj.(eventData.Name) = obj.registry.ReadValue(eventData.Name); %read value in registry
end
end
end
Question
Is it possible to assign set/get functions in the class constructor?

Respuestas (3)

Sean de Wolski
Sean de Wolski el 6 de Abr. de 2021
  1 comentario
Collin Smith
Collin Smith el 6 de Abr. de 2021
Hi Sean, thank you for taking the time to respond.
The properties are definately dependent; I just don't want to explicitly write the set and get methods for each property of the class. I was hoping to assign them to a single handle in the class constructor.

Iniciar sesión para comentar.


Collin Smith
Collin Smith el 6 de Abr. de 2021
I'd be interested if someone had a better solution to this, but I found a work-around by using listeners
classdef ExampleClass < handle
properties(SetObservable,GetObservable)
prop1;
prop2;
prop3; %etc.
end
properties(Access = private)
disablelistener=0; %used to prevent recursive calling of the set/get listener functions
end
methods
function obj = ExampleClass(varargin)
obj.registry = Registry(); %Defined elsewhere
proplist = fields(obj);
for ii = 1:length(proplist), addlistener(obj,proplist{ii},'PreGet', @obj.ListenerPullValues); end
for ii = 1:length(proplist), addlistener(obj,proplist{ii},'PostSet', @obj.ListenerPushValues); end
end
end
methods(Access = private)
function ListenerPushValues(obj,eventData,~)
if(~obj.disablelistener)
obj.disablelistener=1; %Disable listener function to prevent recursive calling of get/set functions
obj.registry.WriteValue(eventData.Name,obj.(eventData.Name)); %write value in registry
obj.disablelistener=0; %Reenable listner function
end
end
function ListenerPullValues(obj,eventData,~)
if(~obj.disablelistener)
obj.disablelistener=1; %Disable listener function to prevent recursive calling of get/set functions
obj.(eventData.Name) = obj.registry.ReadValue(eventData.Name); %read value in registry
obj.disablelistener=0; %Reenable listner function
end
end
end
end

Steven Lord
Steven Lord el 6 de Abr. de 2021
If prop1, prop2, prop3, etc. are properties in this Registry object that you want to expose as properties of the ExampleClass class, take a look at dynamic properties.

Categorías

Más información sobre Class File Organization en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by