How to add set/get methods methods in class constructor

조회 수: 13 (최근 30일)
Collin Smith
Collin Smith 2021년 4월 6일
답변: Steven Lord 2021년 4월 6일
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?

답변 (3개)

Sean de Wolski
Sean de Wolski 2021년 4월 6일
  댓글 수: 1
Collin Smith
Collin Smith 2021년 4월 6일
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.

댓글을 달려면 로그인하십시오.


Collin Smith
Collin Smith 2021년 4월 6일
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 2021년 4월 6일
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.

카테고리

Help CenterFile Exchange에서 Class File Organization에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by