當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JavaScript面向对象编程之Singleton类
生活随笔
收集整理的這篇文章主要介紹了
JavaScript面向对象编程之Singleton类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在C#、Java等純面向對象的語言中,合理使用設計模式將會使我們的模塊設計和代碼編寫更加健壯和清晰。目前JavaScript的編寫已經從自身的object-based,被逐漸模擬來很象(至少七八分吧)object-oriented的語言了,所以我們也可以遵照一些設計模式的概念來編寫JS代碼。
??? 單態(Singleton)是設計模式中最簡的模式了,所以我們先拿它開刀。關于什么是Singleton,可以簡單參看Implementing the Singleton Pattern in C#,要系統了解屬于就屬于設計模式的范疇了,不是本文要講解的內容。
??? 不過對于C#,當然也包括Java等其它純面向對象語言,由于其類的構造函數(constructor)不是一個普通的函數(不能自定義其返回值),所以它們在編寫Singleton類時都需要使用一個static的屬性或方法來獲取對象的實例。而JavaScript中類的constructor就是一個普通的函數,我們可以改變它的返回值來實現對象實例的返回,而不依賴于語言機制。這是到底是什么意思呢?先看一下JS的Singleton類的實現就明白了,示例代碼如下: <script?language="javascript">
function?Singleton()
{
????//?template?code?for?singleton?class.
????if?(?this.constructor.instance?)
????{
?????????return?this.constructor.instance;
????}?
????else?this.constructor.instance?=?this;
????/**//**//////
????
????this.value?=?parseInt(Math.random()*1000000);
???
????this.toString?=?function()
????{
?????????return?'[class?Singleton]';
????}
}
Singleton.prototype.GetValue?=?function()
{
????return?this.value;
};
Singleton.prototype.SetValue?=?function(value)
{
????this.value?=?value;
};
</script>
??? 前面說的"改變它的返回值來實現對象實例的返回",就是指的在JavaScript類的constructor類可以return this.constructor.instance;。所以JavaScript實現的Singleton類在使用時只管new就行了,而不用使用ClassName.Instance或ClassName.GetInstance()這樣的語法。
??? 類Singleton的測試代碼如下: ?var?singleton?=?new?Singleton();
?alert(__typeof__(singleton));
?alert(singleton.GetValue());
?var?singleton?=?new?Singleton();
?alert(singleton.GetValue());
?singleton.SetValue(1000000);
?var?singleton?=?new?Singleton();
?alert(singleton.GetValue());
?var?singleton?=?new?Singleton();
?alert(singleton.GetValue());
??? 返回結果為:Singleton,586606,586606,1000000,1000000。第二個和第三個是random出來的,反正肯定是一樣的兩個數(__typeof__的實現來自這里:獲取JavaScript用戶自定義類的類名稱)。
??? 單態(Singleton)是設計模式中最簡的模式了,所以我們先拿它開刀。關于什么是Singleton,可以簡單參看Implementing the Singleton Pattern in C#,要系統了解屬于就屬于設計模式的范疇了,不是本文要講解的內容。
??? 不過對于C#,當然也包括Java等其它純面向對象語言,由于其類的構造函數(constructor)不是一個普通的函數(不能自定義其返回值),所以它們在編寫Singleton類時都需要使用一個static的屬性或方法來獲取對象的實例。而JavaScript中類的constructor就是一個普通的函數,我們可以改變它的返回值來實現對象實例的返回,而不依賴于語言機制。這是到底是什么意思呢?先看一下JS的Singleton類的實現就明白了,示例代碼如下: <script?language="javascript">
function?Singleton()
{
????//?template?code?for?singleton?class.
????if?(?this.constructor.instance?)
????{
?????????return?this.constructor.instance;
????}?
????else?this.constructor.instance?=?this;
????/**//**//////
????
????this.value?=?parseInt(Math.random()*1000000);
???
????this.toString?=?function()
????{
?????????return?'[class?Singleton]';
????}
}
Singleton.prototype.GetValue?=?function()
{
????return?this.value;
};
Singleton.prototype.SetValue?=?function(value)
{
????this.value?=?value;
};
</script>
??? 前面說的"改變它的返回值來實現對象實例的返回",就是指的在JavaScript類的constructor類可以return this.constructor.instance;。所以JavaScript實現的Singleton類在使用時只管new就行了,而不用使用ClassName.Instance或ClassName.GetInstance()這樣的語法。
??? 類Singleton的測試代碼如下: ?var?singleton?=?new?Singleton();
?alert(__typeof__(singleton));
?alert(singleton.GetValue());
?var?singleton?=?new?Singleton();
?alert(singleton.GetValue());
?singleton.SetValue(1000000);
?var?singleton?=?new?Singleton();
?alert(singleton.GetValue());
?var?singleton?=?new?Singleton();
?alert(singleton.GetValue());
??? 返回結果為:Singleton,586606,586606,1000000,1000000。第二個和第三個是random出來的,反正肯定是一樣的兩個數(__typeof__的實現來自這里:獲取JavaScript用戶自定義類的類名稱)。
轉載于:https://www.cnblogs.com/birdshome/archive/2005/02/27/109617.html
總結
以上是生活随笔為你收集整理的JavaScript面向对象编程之Singleton类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 飞鸟和鱼的故事
- 下一篇: [导入]失败的软件实训课