Singleton mode is one of the most basic and most commonly used patterns in design patterns, characterized by the existence of only one instance of a single object, so that you can ensure that the same global resources are used in all your code.
The way to implement singleton patterns in Java is to first privatize the constructor, create a private static variable to save the instance internally, and then provide the instance to external access through a static method (typically named getinstance). Because of the privatization of the constructor, the outside cannot instantiate the class, but only through getinstance, so there is only one static instance from the beginning to the end.
In JavaScript we can also draw on this idea.
(i) Direct volume of the use of objects
var Single = { "zhangsan", 20}
objects created by direct amounts of objects are indeed one of the simplest examples of implementations, which does guarantee that there is only one single object in the global, but the object created in this way is "portal open" and cannot create its private members (you can, of course, underline the name you want to be a private member before _nameto imply that it is private, but this is not true privatization. In the previous blog we discussed how to create a private static member that can be implemented by closures.
Using closures
varSingle = ( function(){ //Private member varAge=0; function primethod(){ //...}return{//Public memberName"Zhangsan", Getage: function(){ returnAge }, Setage: function(_age){age = _age; } }})();
In the example above, we get an object by executing the function immediately, the object is still unique, but it has private properties and methods.
(ii) objects created through new
Since it is possible to privatize members through closures, we can of course imitate Java to statically instantiate the object and pass it through the GetInstance method, similar to the implementation in Java. The code is as follows:
varSingle = ( function(){ //Unique instance varSingle =New function(){ //Public member This. Name ="Zhangsan"; This. Say = function(){Console.log ( This. name); } }return{getinstance: function(){ returnSingle } }})();varS1 = Single.getinstance ();varS2 = single.getinstance (); Console.log (S1 = = = s2);//true
In fact, the closure of the familiar, the above principles are similar, just the instance as a static private member.
Lazy Loading
Either way, they have a common feature: The Singleton object is created when the script is loaded, and the overhead is great for very large objects, so we can postpone its instantiation until it needs to be loaded, which is called lazy loading.
varSingle = ( function(){ //Unique instance varSingle//Private Constructors function Constructor(){ //Public member This. Name ="Zhangsan"; }return{getinstance: function(){ if(!single) {//If single does not exist, create a singleSingle =NewConstructor (); }returnSingle } }})();
Externally, we access it through the GetInstance method:
var s1 = Single.getInstance();var s2 = Single.getInstance();console.log(s1 === s2); //true
In this way, an instance is created only when the instance is used.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JavaScript single-case mode