javascript的命令模式 和單例模式

var createCommandMoule =(function (){/*建立單體數據模塊的*/服務器

         var _theCommandMoule;性能

         function init(){測試

                var  _CommandMoule = {this

                         /*valueDatas通常是放的鍵值對的對象{key:"url",value:value}*/url

                         valueDatas:[],spa

                         getData:function(keyValue){ //取數據對象

                             var self = this,get

                                 theValue = null,it

                                 valueDatas = self.valueDatas; io

                            /*網上看別人的測試forEach比 for(var i =0;i<valueDatas .length; i++)性能高不少

                            * 本身也測了一下差不高10倍 */         

                            valueDatas.forEach(function(x){

                                if(x.key == keyValue){

                                    theValue = valueDatas[i].value;

                                }

                             });

                             if(theValue == null){

                                 /*正常狀況若是沒有存在數據會去服務器請求數據*/

                                 // theValue = self.queryData(keyValue);

                             }

                            

                             return theValue;

                         },

                         queryData:function(keyUrl){  

                             /*通常這是用來請求數據用得*/

                         },

                         saveData:function(keyValue,value){

                             /*存數據前,先看看這個數據是否存在*/

                             var self = this;

                             if(!(self.getData(keyValue))){

                                 var theValueObject = {

                                     key:keyValue,

                                     value:value

                                 };

                                 self.valueDatas.push(theValueObject);

                             }

                         },

                         deleteData:function(keyValue){ //刪除數據

                             var self = this;

                             var valueDatas = self.valueDatas;

                             valueDatas.forEach(function(x){

                                if(x.key == keyValue){

                                    valueDatas.splice(i);

                                }

                              });

                         },

                         changeValue:function(keyValue,value){

                             var self = this;

                             var valueDatas = self.valueDatas;

                             var ifChange = false;  //若是返回true表示修改爲功了

                             valueDatas.forEach(function(x){

                                if(x.key == keyValue){

                                    valueDatas[i].value = value;

                                     ifChange = true;

                                }

                             });

                             return ifChange;

                         }

                         };

                

                 return _CommandMoule;

                

         }

         return {

             getDataMoule:function(){

                 if(!_theCommandMoule){  //單體模式若是存在了就直接返回

                     _theCommandMoule = init();

                 }

                 return _theCommandMoule;

         }

         };

})();



function letsgo(){

     var aDataMoule = createCommandMoule.getDataMoule();  

    

     aDataMoule.saveData("m1","theValue key is m1");

     var theValue = aDataMoule.getData("m1");

     console.log(theValue);

     var bDataMoule = createCommandMoule.getDataMoule(); 

     /*由於使用單體模式因此aDataMoule 和bDataMoule是一個東西*/

     var bthevalue = bDataMoule.getData("m1");

     /*bDataMoule沒有存m1的數據同樣也能取得*/

     console.log(bthevalue);

     /*保存數據、刪除數據、修改數據只要一個命令就能夠了*/

     aDataMoule.saveData("m2","theValue key is m1");  //保存數據

     aDataMoule.deleteData("m2");  //刪除數據

     console.log(aDataMoule.getData("m2"));  //取沒有的數據,通常去沒有的數據會發出服務器請求參數是url

}


/*

輸出

theValue key is m1 MyCommandMoul.js:80

theValue key is m1 MyCommandMoul.js:85

null MyCommandMoul.js:89


*/

相關文章
相關標籤/搜索