The singleton mode is also known as the list mode, and more is also called the monomer mode. It is a simple but most commonly used design pattern in software design.
The following is an introduction to the single-instance model in Wikipedia:
In the case of Singleton mode, the generation of a singleton class must ensure that only one instance exists, and many times the whole system only needs to have a global object in order to coordinate the behavior of the whole system. For example, in the whole system configuration file, the configuration data has a singleton object for unified reading and modification, other objects need to configure the data also unified through the Singleton object to obtain configuration data, so as to simplify configuration management in a complex environment.
The idea of a singleton pattern is that a class can return a reference to an object (and is always the same) and a method that obtains the instance (a static method, usually using the getinstance name). So when we call this method, if the class holds a reference that returns the reference without being empty, the person creates an instance of the class, and assigns the instance reference to the reference that the class keeps and returns. The constructor of the class is also defined as a private method, so that other functions can use the constructor to instantiate the object, only the static method of the class to get the unique instance of the class.
For JS, great flexibility makes it possible to implement singleton patterns in a variety of ways, using closures to simulate private data, which can be thought of as follows:
var single = (function () {
var unique;
function getinstance () {
if (unique = = = undefined) {
unique = new Construct ();
}
return unique;
}
function Construct () {
// ... Code to generate a single-instance constructor
}
return {
Getinstance:getinstance
}
})();
Above, unique is a reference to the return object, and getinstance is the static method to obtain the instance. Construct is the constructor that creates the instance.
The singleton can be obtained through single.getinstance (), and each invocation gets to the same singleton. This is the effect of the singleton pattern.
However, for JS, it is obvious that the above-mentioned way is too cumbersome, in different scenarios in different ways to achieve the monomer mode is the advantage of JS
Implementation 1: The simplest object literal
var singleton = {
Attr:1,
Method:function () {return this.attr;}
}
var T1 = Singleton;
var t2 = Singleton;
So obviously, T1 = = = t2.
Very simple, and very useful, the disadvantage is that there is no encapsulation, all the property methods are exposed. For some cases where private variables need to be used, it is very weak. Of course, there are some drawbacks to this issue.
Implementation 2: Constructor internal judgment
In fact, the initial JS implementation is a bit similar, but will be placed on the existence of the instance of the class is the decision to put inside the constructor.
function Construct () {
Ensure that only a single case
if (construct.unique!== undefined) {
return construct.unique;
}
Other code
THIS.name = "NYF";
This.age= "24";
Construct.unique = this;
}
var T1 = new Construct ();
var t2 = new Construct ();
Then there are, T1 = = = t2.
is also very simple, nothing but a property to make judgments, but there is no security in this way, once I modified the unique properties of the construct, then the singleton mode is destroyed.
Implementation 3: Closure mode
For the big flexible brand of JS, any problem can find n answers, but let me weigh the pros and cons, the following is a simple way to use closures to implement a single-case pattern, nothing more than a singleton cache will be created.
var single = (function () {
var unique;
function Construct () {
// ... Code to generate a single-instance constructor
}
unique = new Constuct ();
return unique;
})();
Whenever you talk about var t1 = single; var t2 = single; is similar to the object literal method. But the relative safety is a little bit safer, and of course not absolutely.
If you want to use the call single (), then you only need to change the internal return to
return function () {
return unique;
}
The above method can also be done using new method (formalism bright). Of course, this is just a case of closure, you can also determine the existence of a single case in Construct and so on. Various ways can be selected in various situations.
Summarize
In general, the singleton mode is relatively simple in each major mode, but the singleton mode is a more commonly used and useful mode. In JS is particularly prominent (each object literal can be regarded as a single case ~).
Keep in mind that if you strictly need only one instance object's class (although JS does not have the concept of a Class), you should consider using singleton mode.
Using the data cache to store the singleton, as a way to determine whether a single case has been generated, is the primary implementation of the Singleton mode.
Single-Case mode