I’m trying to implement singleton concept in my play application. But before going into singleton concept can we find a class is instantiated, how many times it is instantiated
I’m trying to implement singleton concept in my play application. But before going into
Share
The simplest way would be to include a thread-safe static counter which is updated in your constructor:
Note that this will not show how many instances are currently alive, as it doesn’t decrement the counter on destruction. You could override
finalizeto do that, but I wouldn’t.Also note that this is not the same thing as a singleton by a long chalk. The recommended ways of achieving a singleton are either using a single-value enum:
or like this:
You can use a nested type to achieve lazier initialization if you really need to, but I’ve rarely found that to be worth the complexity – the above patterns have always done me fine.