Singleton pattern is a very popular pattern.It is used when you want a unique instance in your application to handle a functionality and it provides you with a common access point to that unique instance.
You can implement Singleton pattern in javascript in different ways. Here we try to design it the way you usually do in object oreinted language such as C#.
What we will do is
- Define Constructor
- Make it Singleton
- When can we use this pattern
Define Constructor
First of all lets define a constructor for our class.As we discuss it in the previous article, you can create a class constructor by defining a function and using new keyword when you are calling the function. So our code would be something like this:
-
singleton = function(id,name){
-
this.id = id;
-
this.name = name;
-
}
-
var firstInstance = new singleton(1,'Bob');
-
var secondInstance = new singleton(2,'Glen');
singleton%20%3D%20function%28id%2Cname%29%7B%0A%20%20%20%20this.id%20%3D%20id%3B%0A%20%20%20%20this.name%20%3D%20name%3B%0A%7D%0Avar%20firstInstance%20%3D%20new%20singleton%281%2C%27Bob%27%29%3B%0Avar%20secondInstance%20%3D%20new%20singleton%282%2C%27Glen%27%29%3B
This will create an instance of singleton and set its id to 1 and the name property to Bob, and the second instantiation of singleton will set its id to 2 and the name property to Glen.
Make it Singleton
In other languages we used a static property to hold the value of the unique instance. Here we can do the same thing by using function properties.
-
// use singleton.property = something to hold the instance
-
singleton = function(id,name){
-
this.id = id;
-
this.name = name;
-
singleton.instance = this;
-
}
%2F%2F%20use%20singleton.property%20%3D%20something%20to%20hold%20the%20instance%0Asingleton%20%3D%20function%28id%2Cname%29%7B%0A%20%20%20%20this.id%20%3D%20id%3B%0A%20%20%20%20this.name%20%3D%20name%3B%0A%20%20%20%20singleton.instance%20%3D%20this%3B%0A%7D
This will hold the created instance in singleton.instance property. Now all we have to do is to return that instance in the second instantiation.
-
singleton = function(id,name){
-
if(singleton.instance !== undefined)
-
return singleton.instance;
-
this.id = id;
-
this.name = name;
-
singleton.instance = this;
-
}
singleton%20%3D%20function%28id%2Cname%29%7B%0A%20%20%20%20if%28singleton.instance%20%21%3D%3D%20undefined%29%0A%20%20%20%20%20%20%20%20return%20singleton.instance%3B%0A%20%20%20%20this.id%20%3D%20id%3B%0A%20%20%20%20this.name%20%3D%20name%3B%0A%20%20%20%20singleton.instance%20%3D%20this%3B%0A%7D
Let's see how it works.
-
var firstInstance = new singleton(1,'Bob');
-
var secondInstance = new singleton(2,'Glen');
-
alert(secondInstance.name); //yield Bob
-
//As it shows both refer to one instance
-
secondInstance.name = 'Adam';
-
alert(firstInstance.name); //yield Adam
var%20firstInstance%20%3D%20new%20singleton%281%2C%27Bob%27%29%3B%0Avar%20secondInstance%20%3D%20new%20singleton%282%2C%27Glen%27%29%3B%0Aalert%28secondInstance.name%29%3B%20%2F%2Fyield%20Bob%0A%2F%2FAs%20it%20shows%20both%20refer%20to%20one%20instance%0AsecondInstance.name%20%3D%20%27Adam%27%3B%0Aalert%28firstInstance.name%29%3B%20%2F%2Fyield%20Adam
- In first call it checks if singleton.instance is not undefined (which is undefined now) so it will create an instance and set its id and name to 1 and Bob.
- Then it will set instance property of singleton to this by singleton.instance = this. singleton.instance work as static property of a class.
- In the second instantiation it checks if singleton.instance is not undefined (which is defined in previous instantiation) so it will return singleton.instance which refer to previous instance.
As it shows both instance are referring to the same object.
When can we use this pattern
You can use it when you need exactly one instance of a class and it must be initiated by parameters.
For example think about userAgent which is responsible to detect browser typ. This object does not to be instantiated each time.
-
userAgent = function(){
-
if(userAgent.instance !== undefined)
-
return userAgent.instance;
-
this.ns = typeof document.layers != 'undefined';
-
this.ie = typeof document.all != 'undefined';
-
this.ff = typeof document.getElementById != 'undefined';
-
userAgent.instance = this;
-
}
-
var agent = new userAgent()
userAgent%20%3D%20function%28%29%7B%0A%20%20%20%20if%28userAgent.instance%20%21%3D%3D%20undefined%29%0A%20%20%20%20%20%20%20%20return%20userAgent.instance%3B%0A%20%20%20%20this.ns%20%3D%20typeof%20document.layers%20%21%3D%20%27undefined%27%3B%0A%20%20%20%20this.ie%20%3D%20typeof%20document.all%20%21%3D%20%27undefined%27%3B%0A%20%20%20%20this.ff%20%3D%20typeof%20document.getElementById%20%21%3D%20%27undefined%27%3B%0A%20%20%20%20userAgent.instance%20%3D%20this%3B%0A%7D%0Avar%20agent%20%3D%20new%20userAgent%28%29
Tags: Design Patterns | JavaScript
|