MATLAB Answers

How to add set/get methods methods in class constructor

1 view (last 30 days)
Collin Smith
Collin Smith on 6 Apr 2021
Answered: Steven Lord on 6 Apr 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?

Answers (3)

Sean de Wolski
Sean de Wolski on 6 Apr 2021
  1 Comment
Collin Smith
Collin Smith on 6 Apr 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.

Sign in to comment.


Collin Smith
Collin Smith on 6 Apr 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 on 6 Apr 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.

Tags

Community Treasure Hunt

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

Start Hunting!