JavaScript 設(shè)計(jì)模式學(xué)習(xí) Singleton
更新時(shí)間:2009年07月27日 17:41:07 作者:
JavaScript設(shè)計(jì)模式學(xué)習(xí) Singleton
復(fù)制代碼 代碼如下:
/* Basic Singleton. */
var Singleton = {
attribute1: true,
attribute2: 10,
method1: function() {
},
method2: function(arg) {
}
};
單件模式最主要的用途之一就是命名空間:
/* GiantCorp namespace. */
var GiantCorp = {};
GiantCorp.Common = {
// A singleton with common methods used by all objects and modules.
};
GiantCorp.ErrorCodes = {
// An object literal used to store data.
};
GiantCorp.PageHandler = {
// A singleton with page specific methods and attributes.
};
利用閉包在單件模式中實(shí)現(xiàn)私有方法和私有變量:
GiantCorp.DataParser = (function() {
// Private attributes.
var whitespaceRegex = /\s+/;
// Private methods.
function stripWhitespace(str) {
return str.replace(whitespaceRegex, '');
}
function stringSplit(str, delimiter) {
return str.split(delimiter);
}
// Everything returned in the object literal is public, but can access the
// members in the closure created above.
return {
// Public method.
stringToArray: function(str, delimiter, stripWS) {
if(stripWS) {
str = stripWhitespace(str);
}
var outputArray = stringSplit(str, delimiter);
return outputArray;
}
};
})(); // Invoke the function and assign the returned object literal to
// GiantCorp.DataParser.
實(shí)現(xiàn)Lazy Instantiation 單件模式:
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;
}
}
})();
MyNamespace.Singleton.getInstance().publicMethod1();
您可能感興趣的文章:
- JavaScript設(shè)計(jì)模式之工廠模式和構(gòu)造器模式
- JavaScript 設(shè)計(jì)模式 安全沙箱模式
- JavaScript設(shè)計(jì)模式之觀察者模式(發(fā)布者-訂閱者模式)
- JavaScript 設(shè)計(jì)模式之組合模式解析
- javascript設(shè)計(jì)模式之解釋器模式詳解
- JavaScript設(shè)計(jì)模式之原型模式(Object.create與prototype)介紹
- 學(xué)習(xí)JavaScript設(shè)計(jì)模式(鏈?zhǔn)秸{(diào)用)
- JavaScript設(shè)計(jì)模式之單例模式實(shí)例
- 常用的javascript設(shè)計(jì)模式
- JavaScript 設(shè)計(jì)模式學(xué)習(xí) Factory
- JavaScript編程設(shè)計(jì)模式之構(gòu)造器模式實(shí)例分析
相關(guān)文章
javascript面向?qū)ο缶幊?一) 實(shí)例代碼
javascript面向?qū)ο缶幊虒?shí)例代碼,代碼也算比較基礎(chǔ)了,不懂得朋友可以參考腳本之家之前發(fā)布的文章。2010-06-06
詳解new function(){}和function(){}() 區(qū)別分析
只要 new 表達(dá)式之后的 constructor 返回(return)一個(gè)引用對象(數(shù)組,對象,函數(shù)等),都將覆蓋new創(chuàng)建的匿名對象,如果返回(return)一個(gè)原始類型(無 return 時(shí)其實(shí)為 return 原始類型 undefined),那么就返回 new 創(chuàng)建的匿名對象。2008-03-03
javascript 設(shè)計(jì)模式之單體模式 面向?qū)ο髮W(xué)習(xí)基礎(chǔ)
單體是在腳本加載時(shí)創(chuàng)建的,能將一系列有關(guān)聯(lián)的變量和方法組織為一個(gè)邏輯單元,邏輯單元里面的內(nèi)容通過單一的變量進(jìn)行訪問,也是筆記基礎(chǔ)與常用的面向?qū)ο蟮亩x方法。2010-04-04
javascript 面向?qū)ο?實(shí)現(xiàn)namespace,class,繼承,重載
這幾天老大天天嚷嚷要重構(gòu)我們寫的javascript,抱怨代碼太混亂,可讀性差,維護(hù)困難,要求javascript也按面對象的模型來重構(gòu)。2009-10-10
js對象的構(gòu)造和繼承實(shí)現(xiàn)代碼
js對象的構(gòu)造和繼承實(shí)現(xiàn)代碼,學(xué)習(xí)javascript面向?qū)ο蟮呐笥芽梢詤⒖枷隆懗龈僚c復(fù)用的代碼。2010-12-12
Javascript 類與靜態(tài)類的實(shí)現(xiàn)
在Javascript里,對面向?qū)ο蟛]有一個(gè)直接的實(shí)現(xiàn),對于代碼方面也是非常的靈活。
2010-04-04 
