I'm pretty sure that the following question already has a good answer somewhere else, but it's difficult to find since I do not know the "name" of my problem.
I'm designing a class/object/"something" with the following properties:
- It is sort of a lookup table.
- It does not change after initialization.
- It has several non-primitive members.
- It has a complicated initializer function.
- It is the same for the whole program.
- It is parametrized by template parameters.
So this sounds like a static template class:
template <int T>
class LookupTable{
public:
static void init(){
// create entries depending on T
}
private:
static vector<Entries> entries;
}
What I dislike is that I need to call init()
somewhere in my program. So the first question is: How can I make this class fully self-contained, with no need to be explicitly initialized somewhere?
Second part: What is the general design approach to implement such a class? I would be perfectly happy with a link to a good example.
A possible candidate is the singleton. But I have some doubts:
- The singleton considered bad design in many cases. Is it fine for a lookup table as described?
- Singleton is somewhat long notation, since I have to use LookupTable::getInstance()->getEntry(idx)
.