Javascript設計モードの一例モード
5124 ワード
従来の開発エンジニアの目には、一例として一つの種類が一例しかないということを保証し、実現する方法は一般的にインスタンスが存在するかどうかを先に判断し、直接リターンがあれば、存在しないならば作成して戻ってきます.JavaScriptでは、単に名前空間プロバイダとして、グローバル名前空間から唯一のアクセスポイントを提供して、このオブジェクトにアクセスします.
JavaScriptには、単例を実現する方法がたくさんあります.その中で最も簡単なのは対象の字面量を使う方法です.その字面量には多くの属性と方法が含まれています.
方法1:
https://github.com/shichuan/javascript-patterns/blob/master/design-patterns/singleton.html
http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#singletonpatternjavascript
http://www.cnblogs.com/TomXu/archive/2012/02/20/2352817.html
JavaScriptには、単例を実現する方法がたくさんあります.その中で最も簡単なのは対象の字面量を使う方法です.その字面量には多くの属性と方法が含まれています.
var mySingleton = {
property1: "something",
property2: "something else",
method1: function () {
console.log('hello world');
}
};
今後オブジェクトを拡張する場合は、プライベートメンバとメソッドを追加して、これらの変数と関数宣言をカプセル化するためにクローズドを使用してください.暴露したいだけのpublicメンバーと方法は、サンプルコードは以下の通りです.var mySingleton = function () {
/* */
var privateVariable = 'something private';
function showPrivate() {
console.log(privateVariable);
}
/* ( ) */
return {
publicMethod: function () {
showPrivate();
},
publicVar: 'the public can see this!'
};
};
var single = mySingleton();
single.publicMethod(); // 'something private'
console.log(single.publicVar); // 'the public can see this!'
上のコードはいいですが、使う時だけ初期化するとしたら、どうすればいいですか?資源を節約する目的のために、もう一つのコンストラクションでこれらのコードを初期化できます.var Singleton = (function () {
var instantiated;
function init() {
/* */
return {
publicMethod: function () {
console.log('hello world');
},
publicProperty: 'test'
};
}
return {
getInstance: function () {
if (!instantiated) {
instantiated = init();
}
return instantiated;
}
};
})();
/* :*/
Singleton.getInstance().publicMethod();
一例ではどうやって実現されたか分かりましたが、一例ではどのようなシーンに使えばいいですか?実は、単一の例は一般的にシステム間の様々なモードの通信協調に用いられ、以下のコードは単一の例の最適な実践である.var SingletonTester = (function () {
// :
function Singleton(args) {
// args ( )
var args = args || {};
// name
this.name = 'SingletonTester';
// pointX
this.pointX = args.pointX || 6; // ,
// pointY
this.pointY = args.pointY || 10;
}
//
var instance;
var _static = {
name: 'SingletonTester',
//
// Singleton
getInstance: function (args) {
if (instance === undefined) {
instance = new Singleton(args);
}
return instance;
}
};
return _static;
})();
var singletonTest = SingletonTester.getInstance({ pointX: 5 });
console.log(singletonTest.pointX); // 5
その他の実装方法1:
function Universe() {
//
if (typeof Universe.instance === 'object') {
return Universe.instance;
}
//
this.start_time = 0;
this.bang = "Big";
//
Universe.instance = this;
// this
}
//
var uni = new Universe();
var uni2 = new Universe();
console.log(uni === uni2); // true
方法2:function Universe() {
//
var instance = this;
//
this.start_time = 0;
this.bang = "Big";
//
Universe = function () {
return instance;
};
}
//
var uni = new Universe();
var uni2 = new Universe();
uni.bang = "123";
console.log(uni === uni2); // true
console.log(uni2.bang); // 123
方法3:function Universe() {
//
var instance;
//
Universe = function Universe() {
return instance;
};
//
Universe.prototype = this;
//
instance = new Universe();
//
instance.constructor = Universe;
//
instance.start_time = 0;
instance.bang = "Big";
return instance;
}
//
var uni = new Universe();
var uni2 = new Universe();
console.log(uni === uni2); // true
//
Universe.prototype.nothing = true;
var uni = new Universe();
Universe.prototype.everything = true;
var uni2 = new Universe();
console.log(uni.nothing); // true
console.log(uni2.nothing); // true
console.log(uni.everything); // true
console.log(uni2.everything); // true
console.log(uni.constructor === Universe); // true
方式4:var Universe;
(function () {
var instance;
Universe = function Universe() {
if (instance) {
return instance;
}
instance = this;
//
this.start_time = 0;
this.bang = "Big";
};
} ());
//
var a = new Universe();
var b = new Universe();
alert(a === b); // true
a.bang = "123";
alert(b.bang); // 123
参考資料https://github.com/shichuan/javascript-patterns/blob/master/design-patterns/singleton.html
http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#singletonpatternjavascript
http://www.cnblogs.com/TomXu/archive/2012/02/20/2352817.html