What's the best programming approach to this problem (modules and dictionaries)

I have a module containing several classes. The purpose of the module is to deal with reading a certain complex file type (DICOM, the medical imaging file type).

One of the requirements I need from a class in the module is to match the tag of an element in the file with it’s value representation. Essentially, there are about 2000 different tags, each with one of about 30 different value representations.

I’m trying to figure out the fastest way to look up the value representations of these different tags. One option is to have 2000 constants in the module or the parent class (DICOMObject) and then have a giant select case block that takes the tag as a String and returns it’s value representation. Obviously a Dictionary seems a more sensible approach as I could have each of the unique 2000 types as a key in the Dictionary and the value representation as its corresponding Dictionary value.

My question is this: is it possible to have a shared Dictionary or static Dictionary such that I don’t have to instantiate more than one instance of this large Dictionary in the app? I know I could have a global Dictionary in the app to do this but I’m trying to follow best programming practices and I want to share this module with others in the future so I’d like to have everything contained in the module if possible.


You could make the dictionary a private property of the module
Then you can add methods to the module that are whatever scope that are the only sanctioned way to get data from the dictionary
The first time any of them is called the method creates & initializes the dictionary.
And you can then make sure that it only get created & initialized once.
The problem is that other methods IN the module can still just reach into it.

Or you can make a singleton class that does about the same, has no constructor, only a shared factory method for getting the instance ( which you can create as a static so you only create it once) and that instance has a private shared property that is the dictionary. That class can then have instance methods (or shared methods) for getting the data out of it.

Now nothing outside that class can touch the dictionary except through the API the class provides

And that class can be inside your module

I would make it a class rather than a module. That way, you can have more than one file open at a time.

@Norman Palardy Thanks for the thorough explanation. I think I will attempt the singleton class approach. Really good suggestion.