I'm not a big fan to ask to write code at the interviews. But I still find it useful to do some coding exercises at the whiteboard. One of my favourites is the Singleton pattern. Because Singleton is so simple, you can use it as a starter for so many interesting discussions.
it often comes down to the discussions about the Singleton being lazy or eager. And while it leads to the discussion about Java Memory Model, it's not the most interesting one. No one understands Java Memory Model anyway :)
BTW, did you know that a single-element enum type is the best way to implement a Singleton?
Yes! And you can't imagine how many people do fail with this. If you deploy 2 web applications with the same Singleton class, will there be two instances of the same Singleton or one? Of course, there isn't one true answer for this question - you have to ask the details. The the answer depends much on how the class is loaded. If the class is packaged within the WARs, then you get 2 instances of the Singleton.
This is why Singleton is such a great interview question - it opens a lot of topics for further discussion!
Post a Comment