在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ HTML/ 單件模式
備忘錄模式
解釋器模式
類似 Python 的 zip 函數(shù)
類變量和實例變量
提示參數(shù)
指數(shù)對數(shù)運算
檢查變量的類型是否為數(shù)組
由數(shù)組創(chuàng)建一個字符串
生成隨機數(shù)
刪除數(shù)組中的相同元素
大寫單詞首字母
雙向服務(wù)器
類的混合
計算復(fù)活節(jié)的日期
轉(zhuǎn)換弧度和度
找到上一個月(或下一個月)
雙向客戶端
橋接模式
嵌入 JavaScript
AJAX
觀察者模式
克隆對象(深度復(fù)制)
一個隨機整數(shù)函數(shù)
清理字符串前后的空白符
歸納數(shù)組
平方根倒數(shù)快速算法
適配器模式
打亂數(shù)組中的元素
將數(shù)組連接
使用數(shù)組來交換變量
更快的 Fibonacci 算法
服務(wù)器
服務(wù)端和客戶端的代碼重用
客戶端
查找子字符串
策略模式
CoffeeScrip 的 type 函數(shù)
由數(shù)組創(chuàng)建一個對象詞典
回調(diào)綁定
工廠方法模式
映射數(shù)組
當(dāng)函數(shù)括號不可選
生成可預(yù)測的隨機數(shù)
不使用 jQuery 的 Ajax 請求
把字符串轉(zhuǎn)換為小寫形式
類方法和實例方法
擴展內(nèi)置對象
定義數(shù)組范圍
MongoDB
匹配字符串
創(chuàng)建一個不存在的對象字面值
列表推導(dǎo)
比較范圍
修飾模式
檢測每個元素
拆分字符串
字符串插值
對象數(shù)組
去抖動函數(shù)
使用 Nodeunit 測試
SQLite
單件模式
篩選數(shù)組
替換子字符串
數(shù)組最大值
計算(美國和加拿大的)感恩節(jié)日期
找到一個月中的最后一天
計算兩個日期中間的天數(shù)
基本的 HTTP 服務(wù)器
把字符串轉(zhuǎn)換為大寫形式
使用 HTML 命名實體替換 HTML 標(biāo)簽
For 循環(huán)
模板方法模式
重復(fù)字符串
使用 Jasmine 測試
對象的鏈?zhǔn)秸{(diào)用
數(shù)學(xué)常數(shù)
反轉(zhuǎn)數(shù)組
計算月球的相位
使用 Heregexes
查找子字符串
生成器模式
遞歸函數(shù)
HTTP 客戶端
創(chuàng)建 jQuery 插件
檢測與構(gòu)建丟失的函數(shù)
生成唯一ID
命令模式

單件模式

問題

許多時候你想要一個,并且只要一個類的實例。比如,你可能需要一個創(chuàng)建服務(wù)器資源的類,并且你想要保證使用一個對象就可以控制這些資源。但是使用時要小心,因為單件模式可以很容易被濫用來模擬不必要的全局變量。

解決方案

公有類只包含獲得一個實例的方法。實例被保存在該公共對象的閉包中,并且總是有返回值。

這很奏效因為 CoffeeScript 允許你在一個類的聲明中定義可執(zhí)行的狀態(tài)。但是,因為大多數(shù) CoffeeScript 編譯成一個 IIFE 包,如果這個方式適合你,你就不需要在類的聲明中放置私有的類。之后的內(nèi)容可能對開發(fā)模塊化代碼有所幫助,例如 CommonJS(Node.js)或 Require.js 中可見(見實例討論)。

class Singleton
  # You can add statements inside the class definition
  # which helps establish private scope (due to closures)
  # instance is defined as null to force correct scope
  instance = null
  # Create a private class that we can initialize however
  # defined inside this scope to force the use of the
  # singleton class.
  class PrivateClass
    constructor: (@message) ->
    echo: -> @message
  # This is a static method used to either retrieve the
  # instance or create a new one.
  @get: (message) ->
    instance ?= new PrivateClass(message)

a = Singleton.get "Hello A"
a.echo() # => "Hello A"

b = Singleton.get "Hello B"
b.echo() # => "Hello A"

Singleton.instance # => undefined
a.instance # => undefined
Singleton.PrivateClass # => undefined

討論

通過上面的實例我們可以看到,所有的實例是如何從同一個 Singleton 類的實例中輸出的。你也可以看到,私有類和實例變量都無法在 Singleton class 外被訪問到。 Singleton class 的本質(zhì)是提供一個靜態(tài)方法得到只返回一個私有類的實例。它也對外界也隱藏私有類,因此你無法創(chuàng)建一個自己的私有類。

隱藏或使私有類在內(nèi)部運作的想法是更受偏愛的。尤其是由于缺省的 CoffeeScript 將編譯的代碼封裝在自己的 IIFE(閉包)中,你可以定義類而無須擔(dān)心會被文件外部訪問到。在這個實例中,注意,用慣用的模塊導(dǎo)出特點來強調(diào)模塊中可被公共訪問的部分。(請看 “導(dǎo)出到全局命名空間” 中對此理解更深入的討論)。

root = exports ? this

# Create a private class that we can initialize however
# defined inside the wrapper scope.
class ProtectedClass
  constructor: (@message) ->
  echo: -> @message

class Singleton
  # You can add statements inside the class definition
  # which helps establish private scope (due to closures)
  # instance is defined as null to force correct scope
  instance = null
  # This is a static method used to either retrieve the
  # instance or create a new one.
  @get: (message) ->
    instance ?= new ProtectedClass(message)

# Export Singleton as a module
root.Singleton = Singleton

我們可以注意到 coffeescript 是如此簡單地實現(xiàn)這個設(shè)計模式。為了更好地參考和討論 JavaScript 的實現(xiàn),請看初學(xué)者必備 JavaScript 設(shè)計模式。