Singleton mode: Guarantees that a class has only one instance and provides a global access point to access it.
To implement a standard singleton pattern, use a variable to flag whether an object is currently created for a class, or, if so, to return directly to the object created before the next time the object instance is fetched
Example:
1 varSingleton =function(name) {2 This. Name =name;3 This. Instance =NULL;//use a variable to indicate whether the object has already been created4 };5 6Singleton.prototype.getName =function(){7Console.log ( This. Name);8 };9 TenSingleton.getinstance =function(name) { One if(! This. Instance) { A This. Instance =NewSingleton (name); - } - return This. instance; the }; - - varA = Singleton.getinstance ("Sven1"); - varb = singleton.getinstance ("Sven2"); + -Console.log (A = = B);//true +Console.log (A.getname () + b.getname ());//sven1 sven1
Or another way of writing:
1 varsingleton=function(name) {2 This. Name =name;3 };4Singleton.prototype.getName =function(){5Console.log ( This. Name);6 };7 8Singleton.getinstance = (function(){9 varInstance =NULL;//whether the object's flag has been createdTen return function(name) { One if(!instance) { AInstance =NewSingleton (name); - } - returninstance; the }; - })(); - - varA = Singleton.getinstance ("a"); + varb = Singleton.getinstance ("b"); - +Console.log (A = = B);//true AConsole.log (A.getname () + b.getname ());//a A
Second, the use of agents to achieve simple interest mode
In the following example, you will use the Creatediv Singleton class, which is responsible for creating a unique div node in the page
1 //Create Div2 varCreatediv =function(HTML) {3 This. html =html;4 This. Init ();5 };6CreateDiv.prototype.init =function(){7 vardiv = document.createelement ("div");8div.innerhtml = This. html;9 Document.body.appendChild (div);Ten }; One A //single-instance mode control class Proxysingletoncreatediv - varProxysingletoncreatediv = (function(){ - varInstance =NULL; the return function(HTML) { - if(!instance) { -Instance =Newcreatediv (HTML); - } + returninstance; - } + })(); A at varA =NewProxysingletoncreatediv ("Sven1"); - varb =NewProxysingletoncreatediv ("Sven2"); -Console.log (A===B);
Single-instance mode in JavaScript
The core of the simple interest pattern is to ensure that there is only one example and provide global access
Global variables are not singleton patterns, but we can use global variables as simple interest mode, for example:
var g = {};
Global variables tend to cause namespace pollution and should minimize the use of global variables. Ways to reduce global pollution:
1. Using namespaces
var namespace1 = { function() { console.log (1); }, function () { Console.log (2); } };
2. Encapsulating private variables with closures
var user = (function() { var _name = "Sven", =; return { function() { return _name + "-" + _age;}}} ) ();
Private variables are encapsulated in the scope of the closure, and no external access to these two variables can prevent contamination of global commands.
Iv. Inert single case
It means that you can create an object instance when we need it. The lazy Singleton is the focus of the singleton pattern.
Example: In this example, we put the responsibility of creating the instance object and the responsibility of the management object separately in two methods, the two methods can change independently and do not affect each other, when they are linked together, to complete the creation of a unique power object function, it seems to be a very wonderful thing.
1 //managing logic code for a single case2 varGetsingle =function(FN) {3 varresult;4 return function(){5 returnResult | | (Result = Fn.apply ( This, arguments))6 }7 };8 9 //constructor FunctionTen varCreateloginlayer =function(){ One vardiv = document.createelement ("div"); Adiv.innnerhtml = "I am the landing window"; -Div.style.display = "None"; - Document.body.appendChild (div); the returnDiv; - }; - - varCreatesingleloginlayer =Getsingle (Createloginlayer);//methods for creating instances +document.getElementById ("Loginbtn"). onclick =function(){ - varLoginlayer =Createsingleloginlayer (); +LoginLayer.style.display = "Block"; A};
JavaScript design mode-Singleton mode