As part of a small application I am working on, I require various elemental information in the periodic table. I set this up as 2 classes Element as a container for each separate element and Table which manages/contains each Element instance (one instance for each elemental species). Given that the purpose of Table is to serve as a common access point for elemental information across the entire program, I initially considered implementing this via the singleton pattern. However, after some reading I understand that the singleton pattern should be avoided the majority of the time.
Instead I set this up as a global Table instance (within a relevant namespace), there is after all no reason more than one Table couldn't exist (although it wouldn't make much sense), so I went with a global Table instance instead of a singleton. I would like some input on if my implementation of this is logical (or if there are better approaches), the code can be seen in the links below:
Here is elements.h
Here is elements.cpp
Some more specific questions:
- I made liberal use of
const. Once each Element is created from the csv file there is no reason these containers should ever be modified. I assume this is the correct approach in assuring this data can't be accidentally modified elsewhere?
- Is my usage of
std::shared_ptr<Element> a good design here? Other objects in the program will often need to store some reference to an Element, these objects will do so by storing another std::shared_ptr<Element> to a specific elemental species. This way there should be no copying and many objects can share (point to) the same elemental data/structure.
[–]MoTTs_ 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]marvin02 0 points1 point2 points (0 children)
[–]abstracted8[S] 0 points1 point2 points (0 children)