MATLAB Answers

proper namespace organisation with classes and enums

2 views (last 30 days)
Dimitrii Nikolaev
Dimitrii Nikolaev on 26 Mar 2021
Edited: Dimitrii Nikolaev on 26 Mar 2021
Hi Folks,
I'm curious of how to proper organise a folder structure for xx
Setting
Consider I'm developing a driver class for some DeviceA made by Manufacturer.
I assume, that I will use this DeviceA in several more Projects and I probably will get another one DeviceB, which use the same manufacturer-specific enumerated definitions of something - let`s say the volume levels will be defined as:
classdef enumVolumeLevels < uint8
enumeration
volLow (0)
volMid (50)
volHigh (100)
end
end
So I will write some kine of driver class including everything manufacturer-specific either as constants or as enumerations.
Folder Structure
So my Folderstructure will look like:
%rootfolder:
.\+Manufacturer\@DeviceA\DeviceA.m % class definition including constructor and destructor methods and some property 'Volume'
.\+Manufacturer\@DeviceA\SomeMethod.m % Methods will be stored separately in class folder
.\+Manufacturer\enumVolumeLevels.m % enumeration class containing manufacturer-specific definitions of volume levels
Now if I'm going to write a setter and getter method, which both checks If the provided Value fits to the manufacturer-defined levels
classdef DeviceA
% [...]
methods
%getter
function value = get.Volume(obj)
value = Manufacturer.enumVolumeLevels(50); %some dummy getter method
end
%setter
function set.Volume(obj,level)
arguments
obj
mode Manufacturer.enumVolumeLevels
end
% some dummy setter method
end
end
%[...]
end
Standalone usage
Now I'm goind to test my class from the root folder of this project.
... and works fine if I define an object of this class and assign or read the property Volume
test = Manufacturer.DeviceA(); %call constructor
test.Volume = 50; %set property to something
disp(test.Volume); % display this property
Code Refactoring
Following the initial Idea of writing this driver class to use it in several Application specific projects I will include it into root folder of ProjectA, which looks like:
%rootfolder of ProjectA:
.\+Drivers\+Manufacturer\@DeviceA\DeviceA.m % class definition including constructor and destructor methods and some property 'Volume'
.\+Drivers\+Manufacturer\@DeviceA\SomeMethod.m % Methods will be stored separately in class folder
.\+Drivers\+Manufacturer\enumVolumeLevels.m % enumeration class containing manufacturer-specific definitions of volume levels
.\MyFunc.m %some script containing instancesof the DeviceA-class
But if I write MyFunc.m as follows:
function MyFunc
test = Drivers.Manufacturer.DeviceA(); % call constructor
test.Volume = 50; % set property to something
disp(test.Volume); % display this property
end
... it will fail after the 2nd line because from the setter point of view the enumeration class Manufacturer.enumVolumeLevels is not defined.
So my question is:
--> Is there a solution for organizing my namespace folders to avoid such visibility Problem?
Perhaps I'll need to chanhe the folder structure of my Manufacturer-package or the way I use the enumeration class in my class DeviceA, but I'm completely stuck right now and have no Idea, what should be the best practice in this case,

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by