I have in my project two classes for managing memory: MemoryStorage
and MemoryFile
.
The first works similar to a HDD and keeps MemoryFiles
(files, directories, etc.) by using a singleton (add, remove, free, etc). The second works like fstream
, but for memory (open, close, write, read, etc.).
Use Example
MemoryFile file(&MemoryStorage);
// Open file in write mode
if (file.open("filename", open_mode::out)) {
// Data file
int* obj = new int;
*obj = 0;
// Write in internal buff
file.write(obj, sizeof(int));
delete obj;
// Put buff in MemoryStorage and clear buff
file.close();
}
// Other moment
// Open file in read mode
if (file.open("filename", open_mode::in)) {
// Data file
int* obj = new int;
// Write in internal buff
file.read(obj, sizeof(int));
cout << *obj << endl; // Print: 0
delete obj;
// Put buff in MemoryStorage and clear buff
file.close();
}
If I don't use a singleton, creating files with the same name in memory will be possible, leading to an inconsistent file system.
I've read several articles in stack overflow like this one that talk about how singleton is bad pattern according to many programmers.
How else do I resolve the programming problem above without a singleton pattern? Do I use a private static member in MemoryStorage
that replicates the effect of a singleton?
Aucun commentaire:
Enregistrer un commentaire