How to store data and allow access (not copying the data) for structured parameter objects?
2 次查看(过去 30 天)
显示 更早的评论
Here is a simplified example project to explain my needs:
I am using several objects to validate and store parameters. Let's say I want to build a human.
I would have an arm class to store all the data:
classdef arm
properties
% property structs and their substructs have variable types/number of fields-> depend on inputs
Muscle (1,1) struct % contains substructs such as biceps with the different subtypes of muscle fibres
Bone (1,1) struct % contains substructs
end
methods
function obj = set.Muscle(obj, muscle)
% some parsing/validation code
end
function obj = set.Bone(obj, bone)
% some parsing/validation code
end
end
end
The object of the arm class is the property of a human class:
classdef human
properties
Arm (1,1) arm
Core (1,1) core
Leg (1,1) leg
end
methods
function obj = set.Arm(obj, arm)
% some parsing/validation code
end
function obj = set.Core(obj, core)
% some parsing/validation code
end
function obj = set.Leg(obj, leg)
% some parsing/validation code
end
end
end
However, some parameters depend on the DNA of the human (stored in an external textfile) which must be loaded/read and "stored" on a filehandle for the validation/parsing process.
I do not want to store the loaded DNA data from the textfile in every object (due to performance issues- the data assignment is done for many bodyparts and human instances) but at a central place from which all of the objects do have access to but cannot change the data.
What is the best way to ensure this? Thank you very much in advance!
0 个评论
回答(1 个)
Sameer
2024-2-28
Hi,
From my understanding, you have a code where you're creating instances of human anatomy with classes like arm, human, etc., and you want to validate and store parameters for different body parts. However, these parameters are dependent on DNA data that is stored externally in a text file. You wish to access this DNA data across different objects without duplicating it in each instance to avoid performance issues, ensuring that the data can be read but not modified by these objects.
To ensure that all objects can access the DNA data without the need to store it in every object, you can use a Singleton pattern. A Singleton is a design pattern that ensures a class has only one instance and provides a global point of access to that instance.
In MATLAB, you can implement a Singleton for the DNA data as follows:
classdef DNAData
properties (Access = private)
Data
end
methods (Access = private)
function obj = DNAData()
% Private constructor for Singleton pattern
obj.Data = obj.loadDNAData();
end
function data = loadDNAData(obj)
% Load the DNA data from a file
fid = fopen('dna.txt', 'r');
if fid == -1
error('Cannot open DNA data file.');
end
data = fread(fid, '*char')';
fclose(fid);
end
end
methods (Static)
function obj = instance()
% Static method to get the Singleton instance
persistent uniqueInstance;
if isempty(uniqueInstance)
uniqueInstance = DNAData();
end
obj = uniqueInstance;
end
end
methods
function data = getDNAData(obj)
% Public method to access the DNA data
data = obj.Data;
end
end
end
With this setup, any object that needs to access the DNA data can call DNAData.instance().getDNAData() to get the data without needing to store it within the object itself. Here is an example of how you might use it within the arm class:
classdef arm
properties
Muscle (1,1) struct
Bone (1,1) struct
end
methods
function obj = set.Muscle(obj, muscle)
dnaData = DNAData.instance().getDNAData();
% some parsing/validation code that uses dnaData
end
function obj = set.Bone(obj, bone)
dnaData = DNAData.instance().getDNAData();
% some parsing/validation code that uses dnaData
end
end
end
The "DNAData" class is responsible for managing the DNA data, and it ensures that there is only one copy of the data in memory. The instance method guarantees that the same instance is returned every time it is called, and the "getDNAData" method provides a way for other objects to read the data. Since there are no public methods that allow modification of the Data property, other objects cannot change the DNA data, ensuring its integrity.
I hope this helps!
Sameer
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!