Introduction
This article presents a basic explanation of when we need a singleton pattern, what is singleton pattern and how to implement it in C++ and C#.
Background
The first thing is when do we need singleton patterns. If we have a system that requires:
- A single instance is required on a subsystem and the subsystem itself is able to enforce the single instance on itself and the rest of the system need not have to worry about it.
- This single instance subsystem should be accessible by the complete system or say by most part of the system. We can say globally accessible too.
- This single instance subsystem should not be created and initialized unless it is required. (We know it as lazy initialization.)
Now these are the three good reasons that any good or bad book refers to as the scenarios when we should use singleton pattern but I will say, even if you have the first requirement then use this pattern.
OK now let us look at that subsystem that requires a single instance and make it a single class, assuming that this subsystem provides unique services to the system. If not, then the first good idea will be to try to break this subsystem into further parts so that all the parts can be packed inside a single classes.
We are saying that the class should provide one and only one instance, so how can that be achieved because any application wanting to create an instance of this class calls its constructor but wait hey constructor so why not restrict the applications from freely accessing the constructor. So the first thing we need to do is to make the constructor private.
We going to create that one instance that is required. OK, for that, let's write some method for this class, say Getinstance which can actually create an object of this class and give it to the applications. Now since we want to use this method on this class to create the objects, we have to make it static.
Good so now we have our singleton class in place and we are happy that we have successfully created a singleton class. And for a C# guy, this assumption is actually true but for a C++ guy, do we really have a singleton class ready. Well the way I see this class has a problem and on the top of the list is that it is not at all singleton.
The reason I am saying this class is not singleton is what of an application does this:
Singleton s(Singleton::Getinstance());
and the default copy semantics in C++ will let the user create many objects of this type so what we need to do next is to prevent the copy operations.
Now we can say we have a fairly good singleton class. This class now shows a perfect example, rudimentary though, of how to create a simple singleton class.
Using the Code
Here is the C++ implementation of the Singleton class:
class Singleton
{
private:
Singleton(void)
{
}
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
public:
~Singleton(void)
{
}
static Singleton& Getinstance()
{
static Singleton instance;
return instance;
}
void LogMessage(std::string msg)
{
std::cout << msg << "\n"; }
};
Here is the C# implementation of the Singleton class:
sealed class Singleton
{
private Singleton()
{
}
private static Singleton instance = null;
private static readonly object myLock = new Object();
~Singleton()
{
}
public static Singleton GetInstance()
{
lock (myLock)
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
public void LogMessage(string msg)
{
Console.WriteLine(msg); }
}
Note: Please refer to the source code for implementation details and test code.
Points of Interest
Although this stuff is very basic and the way I implemented it portrays it like a very small activity, it could really be useful for the guys who are just at the start of their career and are looking forward to learning about programming styles and good design.
History
- 9th February, 2012: Initial version
- 9th February, 2012: Minor changes to ensure lazy initialization and thread safety.