本文簡要介紹ruby語言中 Observable模塊
的用法。
觀察者模式(也稱為發布/訂閱)提供了一種簡單的機製,讓一個對象在其狀態發生變化時通知一組感興趣的第三方對象。
機製
通知類混合在Observable
模塊中,該模塊提供了管理相關觀察者對象的方法。
可觀察對象必須:
-
斷言它有
#changed
-
調用
#notify_observers
觀察者使用 Observable#add_observer
訂閱更新,它還指定通過 notify_observers
調用的方法。 notify_observers
的默認方法是更新。
示例
下麵的例子很好地說明了這一點。 Ticker
運行時,會不斷收到庫存 Price
的 @symbol
。 Warner
是價格的一般觀察者,並演示了兩個警告器,一個 WarnLow
和一個 WarnHigh
,它們分別在價格低於或高於其設定限製時打印警告。
update
回調允許警告器在不被顯式調用的情況下運行。該係統由Ticker
和幾個觀察者組成,觀察者在沒有頂級代碼幹預的情況下履行職責。
請注意,發布者和訂閱者(可觀察者和觀察者)之間的合同未聲明或強製執行。 Ticker
發布時間和價格,警告者收到。但是,如果你不能確保你的合同是正確的,那麽沒有其他東西可以警告你。
require "observer"
class Ticker ### Periodically fetch a stock price.
include Observable
def initialize(symbol)
@symbol = symbol
end
def run
last_price = nil
loop do
price = Price.fetch(@symbol)
print "Current price: #{price}\n"
if price != last_price
changed # notify observers
last_price = price
notify_observers(Time.now, price)
end
sleep 1
end
end
end
class Price ### A mock class to fetch a stock price (60 - 140).
def self.fetch(symbol)
60 + rand(80)
end
end
class Warner ### An abstract observer of Ticker objects.
def initialize(ticker, limit)
@limit = limit
ticker.add_observer(self)
end
end
class WarnLow < Warner
def update(time, price) # callback for observer
if price < @limit
print "--- #{time.to_s}: Price below #@limit: #{price}\n"
end
end
end
class WarnHigh < Warner
def update(time, price) # callback for observer
if price > @limit
print "+++ #{time.to_s}: Price above #@limit: #{price}\n"
end
end
end
ticker = Ticker.new("MSFT")
WarnLow.new(ticker, 80)
WarnHigh.new(ticker, 120)
ticker.run
產生:
Current price: 83 Current price: 75 --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 75 Current price: 90 Current price: 134 +++ Sun Jun 09 00:10:25 CDT 2002: Price above 120: 134 Current price: 134 Current price: 112 Current price: 79 --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 79
與 procs 一起使用
#notify_observers
方法也可以與 +proc+s 一起使用,方法是將 :call
用作 func
參數。
以下示例說明了 lambda 的使用:
require 'observer'
class Ticker
include Observable
def run
# logic to retrieve the price (here 77.0)
changed
notify_observers(77.0)
end
end
ticker = Ticker.new
warner = ->(price) { puts "New price received: #{price}" }
ticker.add_observer(warner, :call)
ticker.run
相關用法
- Ruby Object.instance_variable_get用法及代碼示例
- Ruby Object.display用法及代碼示例
- Ruby Object.remove_instance_variable用法及代碼示例
- Ruby Object.define_singleton_method用法及代碼示例
- Ruby ObjectSpace.memsize_of_all用法及代碼示例
- Ruby Object.methods用法及代碼示例
- Ruby Object.public_send用法及代碼示例
- Ruby ObjectSpace模塊用法及代碼示例
- Ruby Object.xmp用法及代碼示例
- Ruby Object.singleton_methods用法及代碼示例
- Ruby Object.enum_for用法及代碼示例
- Ruby ObjectSpace.count_symbols用法及代碼示例
- Ruby ObjectSpace.define_finalizer用法及代碼示例
- Ruby ObjectSpace.count_imemo_objects用法及代碼示例
- Ruby Object.freeze用法及代碼示例
- Ruby Object.inspect用法及代碼示例
- Ruby Object.obj ==用法及代碼示例
- Ruby Object.method用法及代碼示例
- Ruby Object.DelegateClass用法及代碼示例
- Ruby ObjectSpace.allocation_generation用法及代碼示例
- Ruby Object.instance_of?用法及代碼示例
- Ruby Object.nil?用法及代碼示例
- Ruby Object.instance_variable_defined?用法及代碼示例
- Ruby ObjectSpace.each_object用法及代碼示例
- Ruby ObjectSpace.reachable_objects_from用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Observable模塊。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。