OK so first I must admit that getting up from bed and start typing is a little bit difficult and on the top of that if you ask me to post whatever I have been doing in my study hours along with full code and explanations then I have to say “You have got to be kidding me” I mean, common these are supposed to be my leisure hours.
Anyway, now I am all set and ready to put down my sample implementations along with the code and explanations.
I have been doing some reading from quite some time and the readings are mainly related to C++ and design only. It is because I think once I get hold on these two things I can learn any technology.
OK so coming back to point, I will jot down my understanding of design patterns and sample implementations, if you find it useful do comment on it and if you find my understanding wrong then please tell me where I am wrong.
So I will start with the Singleton Pattern {Common man, this is my first one, you have to give me a little slack, I can choose the simple one to start}
Singleton Pattern
The first thing is need, 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 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 if 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 good three reasons that any good or bad book refers to as the scenarios when we should use singleton pattern but I will say 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 first good idea will be to try to break this subsystem into further parts so that all the parts can be packed inside single classes.
Now we have a class and if we think how can define something that is unique for a class, the answer is making it static. But here we are talking about creating a single object of that class so how static is going to help. Well let us just start putting some code now.
Lets say we have a class Singleton
class Singleton
{
}
Now we are saying that the class should provide only 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 lets make the constructor private. Good idea lets do it.
class Singleton
{
virtual ~Singleton();
private:
Singleton();
}
Good but how are we going to create that one instance that is required. OK for that lets 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. So my class will become
class Singleton
{
public:
virtual ~Singleton();
static Singleton& Getinstance();
private:
Singleton();
};
Now the first thing that popped in my head when I sat to implement this singleton is that ‘why not use the static local variable of type Singleton inside this Getinstance function and create it and return it to the application. Fair enough, lets do that.
class Singleton
{
public:
virtual ~Singleton(){}
static Singleton& Getinstance()
{
static Singleton s;
return s;
}
private:
Singleton(){}
};
Good so now we have our singleton class in place and we are happy that we have successfully created a singleton class. But have we really. Well the way I see this class has a lot of problems and on the top of the list is that it is not at all singleton. Well but I still remember the day when I discovered why this class is not singleton and believe me that was not less that an epiphany for me.
OK you might want to argue that this is singleton (only if you are a beginner like I am, or an alleged C++ programmer with some fake experience) but let me assure you that all the argument in favour of this class are in vain.
The reason I am saying this class is not singleton is that what of an application do this:
Singleton s(Singleton::Getinstance())
Ha ha, so now you agree that this is not singleton. I think you do. So lets make our singleton class really singleton, shall we
class Singleton
{
public:
virtual ~Singleton(){}
static Singleton& Getinstance()
{
static Singleton s;
return s;
}
private:
Singleton(){}
Singleton(Singleton&){}
Singleton& operator=(const Singleton&){return 0}
};
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. This pattern is fairly simple to implement and no one needs to read it to implement but hey I have to start from somewhere. BTW I implemented this pattern long back when I have not even heard of design patterns.
Anyway, now I am all set and ready to put down my sample implementations along with the code and explanations.
I have been doing some reading from quite some time and the readings are mainly related to C++ and design only. It is because I think once I get hold on these two things I can learn any technology.
OK so coming back to point, I will jot down my understanding of design patterns and sample implementations, if you find it useful do comment on it and if you find my understanding wrong then please tell me where I am wrong.
So I will start with the Singleton Pattern {Common man, this is my first one, you have to give me a little slack, I can choose the simple one to start}
Singleton Pattern
The first thing is need, 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 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 if 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 good three reasons that any good or bad book refers to as the scenarios when we should use singleton pattern but I will say 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 first good idea will be to try to break this subsystem into further parts so that all the parts can be packed inside single classes.
Now we have a class and if we think how can define something that is unique for a class, the answer is making it static. But here we are talking about creating a single object of that class so how static is going to help. Well let us just start putting some code now.
Lets say we have a class Singleton
class Singleton
{
}
Now we are saying that the class should provide only 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 lets make the constructor private. Good idea lets do it.
class Singleton
{
virtual ~Singleton();
private:
Singleton();
}
Good but how are we going to create that one instance that is required. OK for that lets 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. So my class will become
class Singleton
{
public:
virtual ~Singleton();
static Singleton& Getinstance();
private:
Singleton();
};
Now the first thing that popped in my head when I sat to implement this singleton is that ‘why not use the static local variable of type Singleton inside this Getinstance function and create it and return it to the application. Fair enough, lets do that.
class Singleton
{
public:
virtual ~Singleton(){}
static Singleton& Getinstance()
{
static Singleton s;
return s;
}
private:
Singleton(){}
};
Good so now we have our singleton class in place and we are happy that we have successfully created a singleton class. But have we really. Well the way I see this class has a lot of problems and on the top of the list is that it is not at all singleton. Well but I still remember the day when I discovered why this class is not singleton and believe me that was not less that an epiphany for me.
OK you might want to argue that this is singleton (only if you are a beginner like I am, or an alleged C++ programmer with some fake experience) but let me assure you that all the argument in favour of this class are in vain.
The reason I am saying this class is not singleton is that what of an application do this:
Singleton s(Singleton::Getinstance())
Ha ha, so now you agree that this is not singleton. I think you do. So lets make our singleton class really singleton, shall we
class Singleton
{
public:
virtual ~Singleton(){}
static Singleton& Getinstance()
{
static Singleton s;
return s;
}
private:
Singleton(){}
Singleton(Singleton&){}
Singleton& operator=(const Singleton&){return 0}
};
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. This pattern is fairly simple to implement and no one needs to read it to implement but hey I have to start from somewhere. BTW I implemented this pattern long back when I have not even heard of design patterns.
No comments:
Post a Comment