Monthly Archives: January 2014

Pattern of the Week: Singleton

This is the first in a (hopefully) long series of “Pattern of the Week” posts. I’m primarily sticking with the Gang of Four patterns, but I may branch off occasionally. Thanks for reading.

Type: Creational
Difficulty: Beginner

Singleton. It is probably the least complicated of all design patterns, yet it can be confusing for beginners. In this post, I will attempt to explain exactly how to create a Singleton, talk about common implementation problems, and provide some sample code so you can see it in action.

The basic idea of a Singleton is simple. I only want one of some object. Well, that’s easy, right? Don’t create more than one. Looks like my work here is done. Well, what if I want to ensure that there is only one instance of a certain class ever created while my application is running? That’s where Singleton comes in. Singleton basically protects you from creating more than one of something. It also only creates the instance the first time you use it, i.e., lazy initialization. Here’s a basic implementation:

The above code is pretty much the simplest way to create a Singleton. It also isn’t thread-safe, and it isn’t very useful (no properties, members, etc.). Before we go on to that, let’s explain what we’re looking at. First, you’ll notice that the class has a private constructor. This is due to the fact that we don’t want people to just do this:

You’ll also notice a private, static variable on the class called _instance. This variable is what actually holds the Singleton instance. Finally, you’ll notice the Instance property. This is where all the work is done. The getter first checks to see if there is already an instance, and if there isn’t, it creates one. Then, it simply returns the instance. This ensures that there is only one.

Now, this is great, right? Not really. What if your application is multi-threaded? Meaning, two threads accessing the Instance property at the same time? Well, thread 1 will hit the if statement, and say, “I need to create an instance.” Maybe thread 2 will hit that same if statement before the instance is created, and it’ll say, “I need to create an instance.” So, it’s actually possible to end up with two threads with two different instances of Singleton. Fixing this is simple though. See the below corrected code:

I’ve added a static lock object that will prevent more than one thread from going through the instantiate + return code. This means that if the threads reach the code at around the same time, the later one will have to wait. So, looks like we’ve solved all the problems with Singleton. Wait, what if I create a derived class? You can’t create a derived class outside of the Singleton class, since the constructor is private, but what if you create one inside of Singleton?

Then, somewhere in your code, say:

Guess what? You’ve just created two instances of a Singleton class! How do we fix it? Simple. Add the sealed modifier on to the Singleton class. Sealed classes cannot be derived. Here’s our final code:

To make this useful, you would simply add properties to the Singleton class, then access them like this:

That’s all! Visit next week for an explanation of the Decorator pattern.

View Sample Code

SimpleLookups 1.5.1, Blog Status

Today, I’m releasing SimpleLookups 1.5.1.

This version is a major rewrite of a lot of the core stuff, so it contains no API changes. Basically, if you used it before, it should still work great for you today. The only new thing is that you can now configure it through the app.config/web.config files. The download package contains an example of how to do that, and I’ll be updating the documentation so that it’s even more clear.

You may ask yourself, what happened to 1.5? Well, as of today, SimpleLookups is now available in NuGet. The version had to be revved due to me accidentally pushing an early version of 1.5 during a test, and NuGet not having a way to replace the earlier version (unless the version number is higher). I’m planning to release the next major revision soon anyway, so it’s not a big deal.

Besides the SimpleLookups release, I’m planning to use this site as more of a how-to blog instead of just a release log. I’ve done a couple posts, but I really want to start getting into writing about what I’m learning. The next blog post will be educational, and not a release announcement. Pending bugs of course.

Anyway, keep learning and have a great day. -R