Part 4 of the design patterns series I have been writing has just been published on DotNetSlackers. This part covers the singleton pattern.
View it - Design Patterns - Part 4
Excellent article! very well written, I thought, however, you could improve your thread safe version by adding an additional check before locking the resource. If the s_instance variable is already set there would be no reason to lock the object. In effect a double check.
if (s_instance == null)
{
lock (s_syncLock)
s_instance = new PrimeMinister();
}
return s_instance;
cheers!
Thanks Mark. Personally I prefer the version provided at the end - the lock-free one.
Glad you enjoyed it ;-)