Bloomberg LP Interview Question
Senior Software Development EngineersCountry: United States
Interview Type: Written Test
Singleton with respect to what?
1. Virtual Machine level?
2. User Level?
3. OS Level?
4. Network Level?
5. WAN level?
6. .. Internet level?
=====
You can have singleton for all of it, really. Perhaps not [6], enforcing is a huge problem, so may be not [6].
-----
Let's see.
1. Given something is reflective, you can not have true singleton. The only way to achieve singleton in JVM is either use static final classes, or use enums.
2. User level Singleton. Try running two windows media player. Try it. Hmm. You can not. We were taught about this one.
3. OS level - there are plenty examples of singleton process - you can implement that as a class - but... well.
You got the idea.
====
Only one instance exists is not even a pattern, it is a property, and not so much easily implemented.
[ stackoverflow.com/questions/12755539/why-is-singleton-considered-an-anti-pattern ]
=========
In short, the singleton pattern makes code more complex, less useful, and a real pain to re-use or test. Eliminating singletons can be tricky, but it’s a worthwhile endeavour.
==========
the only little bit interesting thing about singleton is the initialization with multiple threads. The trick is to first check the instance variable, if it is not null, return it. if it is null, lock a critical section, check again if the instance variable is not null and only initialize if null. Just don't do this with c++ on multi core platforms, because it won't work properly.
- Chris June 19, 2017