JavaScript實現設計模式中的單例模式的一些技巧總結,javascript設計模式

來源:互聯網
上載者:User

JavaScript實現設計模式中的單例模式的一些技巧總結,javascript設計模式

一、使用全域變數儲存單例

這是最簡單的實現方法

function Person(){   this.createTime=new Date(); } var instance=new Person(); function getInstance(){   return instance; } 

載入該js時就建立一個Person對象,儲存到instance全域變數中,每次使用都取這個對象。如果一次都沒使用,那麼建立的這個對象則浪費了,我們可以最佳化一下,

var instance function getInstance(){   if(!instance){     instance=new Person();   }   return instance; } 

這樣,第一次使用時才建立對象。
這個方法的缺點是,instance是全域的變數,在多人合作或者開發週期比較長的情況下,很難保證instance不會被其它代碼修改或覆蓋,很可能到調用的時候,發現instance根本就不是Person對象。
我們考慮下使用閉包來封裝起instance,使它不再是全域變數就可以解決這個問題了

二、閉包建立對象

var getInstance(){ var instance; return function(){     if(!instance){       instance=new Person();     }     return instance;   } }(); 

這樣,instance就被封裝起來了,不用擔心被修改了。
現在通過getInstance()函數可以獲得單例了。新的問題,如果我通過new Person()來建立對象,獲得的還是多個對象,javascript又不可以像java一樣把構造器私人化。那怎麼樣可以讓多次new出來的對象都是一個執行個體呢?

三、建構函式的靜態屬性緩衝執行個體

先看代碼

function Person(){   //如果已經緩衝了執行個體,則直接返回緩衝的執行個體   if(typeof Person.instance==='object'){     return Person.instance;   }   this.createTime=new Date();   //緩衝執行個體   Person.instance=this;   return this; } 

從代碼可以看到,第一次new時,if的條件返回false,會往下走,初始化對象,然後儲存對象到Person.instance這個靜態屬性中。
第二次new 時,if的條件返回true,直接返回Person.instance,不會再往下運行初始化的代碼。所以不管new幾次,返回的都是第一次建立的對象。

這個方法的缺點和方法一的缺點一樣,Person.instance也是公開屬性,有可能會被修改。

我們參考方法二,使用閉包來封裝一個,也許就能解決該問題了

四、重寫建構函式

這個方法要使用閉包,但不能像方法二那麼簡單,我們需要重寫建構函式。

function Person(){   //緩衝執行個體   var instance=this;   this.createTime=new Date();   //重寫建構函式   Person=function(){     return instance;   } } 

第一次new 時,調用原始建構函式先緩衝該執行個體,然後再初始化,同時,重寫該建構函式。以後再new 時,永遠調用不到原始的建構函式了,只能調用到重寫後的建構函式,而這個函數總是返回緩衝的instance.
上面的方法似乎沒什麼問題,但通過下面的測試,可以發現問題

//向原型添加屬性 Person.prototype.prop1=true; var p1=new Person(); //在建立初始化對象後,再次向該原型添加屬性 Person.prototype.prop2=true; var p2=new Person(); //開始測試 console.log(p1.prop1);//結果為true console.log(p2.prop1);//結果為true console.log(p1.prop2);//結果為undefined console.log(p2.prop2);//結果為undefined console.log(p1.constructor===Person);//結果為false console.log(p2.constructor===Person);//結果為false 

我們預期中的結果,應該是全都是true。
分析一下上述測試代碼

Person.prototype.prop1=true;是在原始建構函式的原型下增加了prop1這個屬性,並賦值

而在執行 var p1=new Person();之後,Person這個建構函式已經被重寫了

所以Person.prototype.prop2=true;是在新的原型下增加prop2這個屬性

var p2=new Person(); p2和p1實際上是同一個對象,即原始建構函式建立的對象

所以p1 p2都有prop1這個屬性,而沒有prop2這個屬性

同樣的,p1 p2的constructor指向的也是原始的建構函式,而Person此時已不是原來那個函數了

為了能按預期的結果那樣運行,可以通過一些修改來實現

function Person(){   //緩衝執行個體   var instance=this;   //重寫建構函式   Person=function(){     return instance;   }   //保留原型屬性   Person.prototype=this;   //執行個體   instance=new Person();   //重設建構函式引用   instance.constructor=Person;   //其他初始化   instance.createTime=new Date();    return instance; } 

再運行前面的測試代碼,結果都是true了。

五、惰性載入:
在大型或複雜的項目中,起到了最佳化的作用:那些開銷較大卻很少用到的組件可以被封裝到惰性載入單例中,樣本程式:

/* Singleton with Private Members, step 3. */MyNamespace.Singleton = (function() { // Private members. var privateAttribute1 = false; var privateAttribute2 = [1, 2, 3]; function privateMethod1() {  ... } function privateMethod2(args) {  ... } return { // Public members.  publicAttribute1: true,  publicAttribute2: 10,  publicMethod1: function() {   ...  },  publicMethod2: function(args) {   ...  } };})();/* General skeleton for a lazy loading singleton, step 1. */MyNamespace.Singleton = (function() { function constructor() { // All of the normal singleton code goes here.  // Private members.  var privateAttribute1 = false;  var privateAttribute2 = [1, 2, 3];  function privateMethod1() {   ...  }  function privateMethod2(args) {   ...  }  return { // Public members.   publicAttribute1: true,   publicAttribute2: 10,   publicMethod1: function() {    ...   },   publicMethod2: function(args) {    ...   }  } }})();/* General skeleton for a lazy loading singleton, step 2. */MyNamespace.Singleton = (function() { function constructor() { // All of the normal singleton code goes here.  ... } return {  getInstance: function() {   // Control code goes here.  } }})();/* General skeleton for a lazy loading singleton, step 3. */MyNamespace.Singleton = (function() { var uniqueInstance; // Private attribute that holds the single instance. function constructor() { // All of the normal singleton code goes here.  ... } return {  getInstance: function() {   if(!uniqueInstance) { // Instantiate only if the instance doesn't exist.    uniqueInstance = constructor();   }   return uniqueInstance;  } }})();

六、使用分支單例:
針對特定環境的代碼可以被封裝到分支型單例中,樣本程式:

/* SimpleXhrFactory singleton, step 1. */var SimpleXhrFactory = (function() { // The three branches. var standard = {  createXhrObject: function() {   return new XMLHttpRequest();  } }; var activeXNew = {  createXhrObject: function() {   return new ActiveXObject('Msxml2.XMLHTTP');  } }; var activeXOld = {  createXhrObject: function() {   return new ActiveXObject('Microsoft.XMLHTTP');  } };})();/* SimpleXhrFactory singleton, step 2. */var SimpleXhrFactory = (function() { // The three branches. var standard = {  createXhrObject: function() {   return new XMLHttpRequest();  } }; var activeXNew = {  createXhrObject: function() {   return new ActiveXObject('Msxml2.XMLHTTP');  } }; var activeXOld = {  createXhrObject: function() {   return new ActiveXObject('Microsoft.XMLHTTP');  } }; // To assign the branch, try each method; return whatever doesn't fail. var testObject; try {  testObject = standard.createXhrObject();  return standard; // Return this if no error was thrown. } catch(e) {  try {   testObject = activeXNew.createXhrObject();   return activeXNew; // Return this if no error was thrown.  }  catch(e) {   try {    testObject = activeXOld.createXhrObject();    return activeXOld; // Return this if no error was thrown.   }   catch(e) {    throw new Error('No XHR object found in this environment.');   }  } }})();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.