Sharing variables between classes
29 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Anastasia Vlasova
el 20 de Abr. de 2017
Comentada: Chris Nemecek
el 14 de Nov. de 2024 a las 3:45
Hello all,
I'm writing code to read and write a certain file format. Those are actually my first steps in OOP. The structure of the file is multilevel. First it has a header and tags. Tags might contain several objects inside. The tags as well as the objects inside the tags might have different structure and encoding. I have classes for each of those components: for the file itself, for each of the tag types, for the objects in tags. Each upper level contains the lower levels as parameters.
The problem that bothers me is that the upper level class has the parameters which the lower level classes also need. Like filename. In the first implementation I was just including filename as a parameter to each class, resulting at the end in multiple filename parameters inside the upper file class. It had an advantage though as I could address it inside any of the class methods as obj.filename.
The next try was to use it as an ordinary local variable, but then each of the class method got additional argument. The last step was to use it as a global variable. This basically does what it should and doesn't look too bad as for me, but I have often heard that global variable doesn't correspond to the concept of the OOP, it is not recommended to use it and so on.
So what is the right way to go? Just using persistent variable instead of global? Using handle classes or class inheritance...?
Thank you in advance!
0 comentarios
Respuestas (1)
Vaibhav
el 25 de Jul. de 2024
Hi Anastasia
One approach is to use class properties and inheritance to manage shared parameters like filename. You can create a base class that contains the shared properties and then inherit from this base class in your other classes.
classdef BaseFile
properties
filename
end
methods
function obj = BaseFile(filename)
obj.filename = filename;
end
end
end
classdef Tag < BaseFile
properties
tagData
end
methods
function obj = Tag(filename, tagData)
obj@BaseFile(filename);
obj.tagData = tagData;
end
end
end
classdef File < BaseFile
properties
tags
end
methods
function obj = File(filename, tags)
obj@BaseFile(filename);
obj.tags = tags;
end
end
end
In above example, BaseFile is a base class that holds the filename property. Both Tag and File classes inherit from BaseFile, so they also have access to the filename property.
If you need to modify shared properties and have those changes reflected across all instances, you might want to use handle classes. Handle classes allow you to create references to objects rather than copies.
classdef BaseFileHandle < handle
properties
filename
end
methods
function obj = BaseFileHandle(filename)
obj.filename = filename;
end
end
end
classdef TagHandle < BaseFileHandle
properties
tagData
end
methods
function obj = TagHandle(filename, tagData)
obj@BaseFileHandle(filename);
obj.tagData = tagData;
end
end
end
classdef FileHandle < BaseFileHandle
properties
tags
end
methods
function obj = FileHandle(filename, tags)
obj@BaseFileHandle(filename);
obj.tags = tags;
end
end
end
Using global variables is generally discouraged in OOP because it can lead to code that is difficult to understand and maintain. Instead, consider passing shared parameters through constructors and methods.
Hope this gets you started!
1 comentario
Chris Nemecek
el 14 de Nov. de 2024 a las 3:45
If I wanted to make filename for an instance of TagHandle the same as filename for an instance of BaseFile, how would I do that?
For example:
a = Basefile('test.m'); --> a.filename would return test.m
1) How would I create an instance of TagHandle with the same filename (ie, test.m)?
2) How could I essentially connect the filename properties such that if I update in any instance of any class, all filename properties update to the same thing across all instances?
For example:
a = Basefile('test.m'); --> a.filename would return test.m
b = TagHandle('newTest.m', tagData) --> b.filename would return newTest.m and I want a.filename to be automatically updated to newTest.m as well.
Ver también
Categorías
Más información sobre Methods en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!