I've written about this a few times now (use the search thingy to find where) but I'm still surprised how many people mess up the Singleton pattern.
For instance take:
if (blogSettingsSingleton == null)
{
blogSettingsSingleton = new BlogSettings();
}
return blogSettingsSingleton;
Looks ok, right? But this is a classic poor pattern when dealing with multi-threaded apps. Why? Look at the initial 'if' statement, and think what happens if multiple treads hit this at the same time...one thread could be in the process of creating this object whilst the others are evaluating the condition...
If you're using Singletons PLEASE read this article. The singleton pattern article from Wikipedia has the following excellent implementation of a singleton:
/// <summary>
/// Class implements singleton pattern.
/// </summary>
public class Singleton
{
// Private constructor to avoid other instantiation
// This must be present otherwise the compiler provide
// a default public constructor
private Singleton()
{
}
/// <summary>
/// Return an instance of <see cref="Singleton"/>
/// </summary>
public static Singleton Instance
{
get
{
/// An instance of Singleton wont be created until the very first
/// call to the sealed class. This a CLR optimization that ensure that
/// we have properly lazy-loading singleton.
return SingletonCreator.CreatorInstance;
}
}
/// <summary>
/// Sealed class to avoid any heritage from this helper class
/// </summary>
private sealed class SingletonCreator
{
// Retrieve a single instance of a Singleton
private static readonly Singleton _instance = new Singleton();
/// <summary>
/// Return an instance of the class <see cref="Singleton"/>
/// </summary>
public static Singleton CreatorInstance
{
get { return _instance; }
}
}
}
This is a lazy-initializing, thread safe singleton...does sacrifice *some* performance (see the earlier article for details of this) but it is FAR safer than the original I showed...
Oh, and one last thing...are you REALLY sure you need a Singleton? One horrible pattern I've seen is code is using Singletons to access database connections...this is VERY RARELY required and sacrifices a lot of the benefits which modern databases offer for concurrent access.
UPDATE: Umm...realized that I don't explain what a 'Singleton' is...essentially it's a pattern which limits the access of an object to a single caller at a time. Common uses for Singletons could be accessing files on a file-system where you really want to make sure that only one thread has access at one time. Oh, and to make things even more complicated you'd more commonly want to use a ReaderWriterLock in this scenario to allow many readers or a single writer...only of course you wouldn't...told you it was complicated! Oh, and to further complicate matters in VS2008 and above you'd probably want ReaderWriterLockSlim!
I was reading a story about a 'playable search engine' called Seeqpod being sued under the DMCA safe harbor. I have to admit that I'd never even heard of Seeqpod before this story but it's well pretty interesting! For a while now I've been embedding Youtube music videos but this is a much more interesting step. The engine doesn't (AFAIK) actually host these music files, it just searches them and provides a streaming service. This is a pretty interesting idea...wonder if it'll survive. Of course I guess these files could be pretty volatile, hmm a cool enhancement would be to add audio-fingerprinting and enable multiple sources for the same file...ad-hoc peer to peer and less volatile links. It would also be interesting to combine a concept like this with a truly wireless media player where you could build playlists but have no actual storage on the device apart from a cache for making streaming seamless; taking iPhone a bit further...think of a Zune with a true on-demand streaming radio service which could select musing based on user defined preferences...hmm makes you think!
Well, back at work after my trip back to Scotland for Christmas and Hogmanay (New Years Eve as non-scots call it :-)). Great time seeing family again, I hope I never have to spend Christmas away from them. We have a few traditions every year, Christmas eve at my cousin's, Christmas day at my sisters, Boxing Day at my parents then the wait until Hogmanay when we head to yet another cousins. Then it's basically a week of being drunk and eating 'traditional' food (Steak Pie, Boiled Ham and Beef Stew, all with mushy peas). So, great time, amazing to see how my nephews have grown and just being around everyone again.
Then it was an agonizing flight back to Seattle...I still haven't worked out airmiles but I REALLY want to upgrade to bigger seats, a 6'3" 300lb (see "Pagliacci Pizza below!" man in a standard airline seat for 10 hours is cruel and unusual punishment (especially one such as myself who can't sleep...even after 2 ambien!). Anyway great to be back, went and picked up my cat from her 'Pet Resort' - fantastic place but I could probably have gotten her a motel room for the same price! Also a bit galling that the cat seemed disappointed about leaving!
Anyway, so I'm back at work...can't talk about what we're doing right now for various legal reasons...but it's not that thrilling!
Back to Seattle, didn't miss the traffic, did miss my obscene American Car; which is even more dented after yet another car park run-in...I aim to have black paint on every concrete post in the MS building 16 car park!
Didn't miss American TV...honestly it is ridiculously bad...we have even more channels in the UK (on Sky Satellite) but the content is 100 times better (thank Jebus for Bittorrent....not that I would EVER use it, but theoretically QI and Never Mind The Buzzcocks would be amazing to watch!).
Well, trying to resist phoning the World's best Pizza place (well, better in the couple of dozen world locations I've had pizza in). Really have to try and mediate my intake slightly better...it's a sign when you know the delivery boy by his first name...and you're in the 'Frequent Piers' club...umm...