Introduced
Starting with this chapter, we'll step through the implementation of the various design patterns used in JavaScript, where I'm not going to introduce the theory of the schema itself too much, but only focus on implementation. OK, officially started.
In the eyes of traditional development engineers, a single example is to ensure that a class has only one instance, the method of implementation is generally to determine whether the instance exists or not, if there is a direct return, if there is no existence on the creation and return, which ensures that a class has only one instance object. In JavaScript, a single instance acts as a namespace provider, providing a unique access point from the global namespace to access the object.
Body
In JavaScript, there are many ways to implement a single example, the simplest of which is to use the method of object literal, which can contain a large number of properties and methods:
Copy Code code as follows:
var Mysingleton = {
Property1: "Something",
Property2: "Something Else",
Method1:function () {
Console.log (' Hello World ');
}
};
If you want to extend the object later, you can add your own private members and methods, and then use closures to encapsulate the variables and function declarations inside them. Exposes only the public members and methods you want to expose, the sample code is as follows:
Copy code code as follows:
var Mysingleton = function () {
/* Here declare private variables and methods * *
var privatevariable = ' Something private ';
function Showprivate () {
Console.log (privatevariable);
}
/* Public variables and methods (access to private variables and methods) * *
return {
Publicmethod:function () {
Showprivate ();
},
Publicvar: ' The public can ' this! '
};
};
var single = Mysingleton ();
Single.publicmethod (); Output ' something private '
Console.log (Single.publicvar); Output ' The public can ' this! '
The code above is pretty good, but what if we want to do it only when we're using it? To conserve resources, we can initialize the code in another constructor, as follows:
Copy Code code as follows:
var Singleton = (function () {
var instantiated;
function init () {
/* Here to define a single example code * *
return {
Publicmethod:function () {
Console.log (' Hello World ');
},
Publicproperty: ' Test '
};
}
return {
Getinstance:function () {
if (!instantiated) {
instantiated = init ();
}
return instantiated;
}
};
})();
/* Call the public method to get the instance: * *
Singleton.getinstance (). Publicmethod ();
Know how to achieve a single example, but what kind of a single example to use in the better? In fact, a single example is typically used in communication coordination between various modes of the system, and the following code is a single best practice:
Copy Code code as follows:
var Singletontester = (function () {
Parameters: A collection of parameters passed to a single instance
function Singleton (args) {
Set the args variable to be a received parameter or empty (if not provided)
var args = args | | {};
Set the name parameter
this.name = ' Singletontester ';
Set the value of Pointx
This.pointx = Args.pointx | | 6; Gets from the received parameter, or is set to the default value
Set the value of pointy
This.pointy = Args.pointy | | 10;
}
Instance container
var instance;
var _static = {
Name: ' Singletontester ',
Methods to get instances
Returns an instance of Singleton
Getinstance:function (args) {
if (instance = = undefined) {
Instance = new Singleton (args);
}
return instance;
}
};
return _static;
})();
var singletontest = singletontester.getinstance ({pointx:5});
Console.log (SINGLETONTEST.POINTX); Output 5
Other ways to implement
Method 1:
Copy Code code as follows:
Function Universe () {
Determine if there are instances
if (typeof universe.instance = = = ' object ') {
return universe.instance;
}
Other content
This.start_time = 0;
This.bang = "big";
Cache
Universe.instance = this;
Return this in an implicit
}
Test
var uni = new Universe ();
var uni2 = new Universe ();
Console.log (Uni = = = Uni2); True
Method 2:
Copy Code code as follows:
Function Universe () {
Instances of caching
var instance = this;
Other content
This.start_time = 0;
This.bang = "big";
overriding constructors
Universe = function () {
return instance;
};
}
Test
var uni = new Universe ();
var uni2 = new Universe ();
Uni.bang = "123";
Console.log (Uni = = = Uni2); True
Console.log (Uni2.bang); 123
Method 3:
Copy Code code as follows:
Function Universe () {
Cache instance
var instance;
Re-construct a function
Universe = function Universe () {
return instance;
};
Post-processing prototype properties
Universe.prototype = this;
Instance
Instance = New Universe ();
To reset a constructor pointer
Instance.constructor = Universe;
Other features
Instance.start_time = 0;
Instance.bang = "big";
return instance;
}
Test
var uni = new Universe ();
var uni2 = new Universe ();
Console.log (Uni = = = Uni2); True
Add prototype Properties
Universe.prototype.nothing = true;
var uni = new Universe ();
Universe.prototype.everything = true;
var uni2 = new Universe ();
Console.log (uni.nothing); True
Console.log (uni2.nothing); True
Console.log (uni.everything); True
Console.log (uni2.everything); True
Console.log (Uni.constructor = = = Universe); True
Mode 4:
Copy Code code as follows:
var universe;
(function () {
var instance;
Universe = function Universe () {
if (instance) {
return instance;
}
instance = this;
Other content
This.start_time = 0;
This.bang = "big";
};
} ());
Test code
var a = new Universe ();
var B = New Universe ();
Alert (A = = = B); True
A.bang = "123";
alert (B.bang); 123