2017-01-13 15:08:29 +03:00
|
|
|
# frozen_string_literal: true
|
2003-02-03 16:52:21 +03:00
|
|
|
#
|
2011-05-17 01:53:12 +04:00
|
|
|
# Implementation of the _Observer_ object-oriented design pattern. The
|
2003-07-26 16:53:09 +04:00
|
|
|
# following documentation is copied, with modifications, from "Programming
|
2012-06-14 01:18:29 +04:00
|
|
|
# Ruby", by Hunt and Thomas; http://www.ruby-doc.org/docs/ProgrammingRuby/html/lib_patterns.html.
|
2003-02-03 16:52:21 +03:00
|
|
|
#
|
2011-05-17 01:53:12 +04:00
|
|
|
# See Observable for more info.
|
|
|
|
|
|
|
|
# The Observer pattern (also known as publish/subscribe) provides a simple
|
2003-02-03 16:52:21 +03:00
|
|
|
# mechanism for one object to inform a set of interested third-party objects
|
2009-03-06 06:56:38 +03:00
|
|
|
# when its state changes.
|
2003-02-03 16:52:21 +03:00
|
|
|
#
|
|
|
|
# == Mechanism
|
|
|
|
#
|
2011-05-17 01:53:12 +04:00
|
|
|
# The notifying class mixes in the +Observable+
|
2003-02-03 16:52:21 +03:00
|
|
|
# module, which provides the methods for managing the associated observer
|
|
|
|
# objects.
|
|
|
|
#
|
|
|
|
# The observable object must:
|
2011-05-17 01:53:12 +04:00
|
|
|
# * assert that it has +#changed+
|
|
|
|
# * call +#notify_observers+
|
2003-02-03 16:52:21 +03:00
|
|
|
#
|
2013-11-21 08:44:45 +04:00
|
|
|
# An observer subscribes to updates using Observable#add_observer, which also
|
|
|
|
# specifies the method called via #notify_observers. The default method for
|
|
|
|
# #notify_observers is #update.
|
|
|
|
#
|
2011-05-17 01:53:12 +04:00
|
|
|
# === Example
|
2003-02-03 16:52:21 +03:00
|
|
|
#
|
|
|
|
# The following example demonstrates this nicely. A +Ticker+, when run,
|
2011-05-17 01:53:12 +04:00
|
|
|
# continually receives the stock +Price+ for its <tt>@symbol</tt>. A +Warner+
|
|
|
|
# is a general observer of the price, and two warners are demonstrated, a
|
|
|
|
# +WarnLow+ and a +WarnHigh+, which print a warning if the price is below or
|
|
|
|
# above their set limits, respectively.
|
2003-02-03 16:52:21 +03:00
|
|
|
#
|
|
|
|
# The +update+ callback allows the warners to run without being explicitly
|
|
|
|
# called. The system is set up with the +Ticker+ and several observers, and the
|
|
|
|
# observers do their duty without the top-level code having to interfere.
|
|
|
|
#
|
|
|
|
# Note that the contract between publisher and subscriber (observable and
|
|
|
|
# observer) is not declared or enforced. The +Ticker+ publishes a time and a
|
|
|
|
# price, and the warners receive that. But if you don't ensure that your
|
|
|
|
# contracts are correct, nothing else can warn you.
|
|
|
|
#
|
|
|
|
# require "observer"
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2003-02-03 16:52:21 +03:00
|
|
|
# class Ticker ### Periodically fetch a stock price.
|
|
|
|
# include Observable
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2003-02-03 16:52:21 +03:00
|
|
|
# def initialize(symbol)
|
|
|
|
# @symbol = symbol
|
|
|
|
# end
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2003-02-03 16:52:21 +03:00
|
|
|
# def run
|
2013-09-26 17:09:13 +04:00
|
|
|
# last_price = nil
|
2003-02-03 16:52:21 +03:00
|
|
|
# loop do
|
|
|
|
# price = Price.fetch(@symbol)
|
|
|
|
# print "Current price: #{price}\n"
|
2013-09-26 17:09:13 +04:00
|
|
|
# if price != last_price
|
2003-02-03 16:52:21 +03:00
|
|
|
# changed # notify observers
|
2013-09-26 17:09:13 +04:00
|
|
|
# last_price = price
|
2003-02-03 16:52:21 +03:00
|
|
|
# notify_observers(Time.now, price)
|
|
|
|
# end
|
|
|
|
# sleep 1
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# class Price ### A mock class to fetch a stock price (60 - 140).
|
2013-09-26 17:09:13 +04:00
|
|
|
# def self.fetch(symbol)
|
2003-02-03 16:52:21 +03:00
|
|
|
# 60 + rand(80)
|
|
|
|
# end
|
|
|
|
# end
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2003-02-03 16:52:21 +03:00
|
|
|
# class Warner ### An abstract observer of Ticker objects.
|
|
|
|
# def initialize(ticker, limit)
|
|
|
|
# @limit = limit
|
|
|
|
# ticker.add_observer(self)
|
|
|
|
# end
|
|
|
|
# end
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2003-02-03 16:52:21 +03:00
|
|
|
# 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
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2003-02-03 16:52:21 +03:00
|
|
|
# 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
|
|
|
|
#
|
|
|
|
# Produces:
|
|
|
|
#
|
|
|
|
# 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
|
1998-01-16 15:13:05 +03:00
|
|
|
module Observable
|
2003-02-03 16:52:21 +03:00
|
|
|
|
|
|
|
#
|
2017-01-13 15:08:29 +03:00
|
|
|
# Add +observer+ as an observer on this object. So that it will receive
|
2011-05-17 01:53:12 +04:00
|
|
|
# notifications.
|
|
|
|
#
|
|
|
|
# +observer+:: the object that will be notified of changes.
|
|
|
|
# +func+:: Symbol naming the method that will be called when this Observable
|
|
|
|
# has changes.
|
2003-02-03 16:52:21 +03:00
|
|
|
#
|
2011-05-17 01:53:12 +04:00
|
|
|
# This method must return true for +observer.respond_to?+ and will
|
|
|
|
# receive <tt>*arg</tt> when #notify_observers is called, where
|
|
|
|
# <tt>*arg</tt> is the value passed to #notify_observers by this
|
|
|
|
# Observable
|
2005-09-05 12:29:52 +04:00
|
|
|
def add_observer(observer, func=:update)
|
|
|
|
@observer_peers = {} unless defined? @observer_peers
|
|
|
|
unless observer.respond_to? func
|
2014-08-27 16:21:41 +04:00
|
|
|
raise NoMethodError, "observer does not respond to `#{func}'"
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2005-09-05 12:29:52 +04:00
|
|
|
@observer_peers[observer] = func
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2003-02-03 16:52:21 +03:00
|
|
|
|
|
|
|
#
|
2011-05-17 01:53:12 +04:00
|
|
|
# Remove +observer+ as an observer on this object so that it will no longer
|
|
|
|
# receive notifications.
|
2003-02-03 16:52:21 +03:00
|
|
|
#
|
2011-05-17 01:53:12 +04:00
|
|
|
# +observer+:: An observer of this Observable
|
1998-01-16 15:13:05 +03:00
|
|
|
def delete_observer(observer)
|
2000-04-10 09:48:43 +04:00
|
|
|
@observer_peers.delete observer if defined? @observer_peers
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2003-02-03 16:52:21 +03:00
|
|
|
|
|
|
|
#
|
2011-05-17 01:53:12 +04:00
|
|
|
# Remove all observers associated with this object.
|
2003-02-03 16:52:21 +03:00
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
def delete_observers
|
2000-04-10 09:48:43 +04:00
|
|
|
@observer_peers.clear if defined? @observer_peers
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2003-02-03 16:52:21 +03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Return the number of observers associated with this object.
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
def count_observers
|
2000-04-10 09:48:43 +04:00
|
|
|
if defined? @observer_peers
|
1998-01-16 15:13:05 +03:00
|
|
|
@observer_peers.size
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
2003-02-03 16:52:21 +03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Set the changed state of this object. Notifications will be sent only if
|
|
|
|
# the changed +state+ is +true+.
|
|
|
|
#
|
2011-05-17 01:53:12 +04:00
|
|
|
# +state+:: Boolean indicating the changed state of this Observable.
|
|
|
|
#
|
1999-08-13 09:45:20 +04:00
|
|
|
def changed(state=true)
|
1998-01-16 15:13:05 +03:00
|
|
|
@observer_state = state
|
|
|
|
end
|
2003-02-03 16:52:21 +03:00
|
|
|
|
|
|
|
#
|
2011-05-17 01:53:12 +04:00
|
|
|
# Returns true if this object's state has been changed since the last
|
|
|
|
# #notify_observers call.
|
2003-02-03 16:52:21 +03:00
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
def changed?
|
2000-04-10 09:48:43 +04:00
|
|
|
if defined? @observer_state and @observer_state
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2003-02-03 16:52:21 +03:00
|
|
|
|
|
|
|
#
|
2011-05-17 01:53:12 +04:00
|
|
|
# Notify observers of a change in state *if* this object's changed state is
|
|
|
|
# +true+.
|
|
|
|
#
|
2012-08-21 17:03:30 +04:00
|
|
|
# This will invoke the method named in #add_observer, passing <tt>*arg</tt>.
|
2011-05-17 01:53:12 +04:00
|
|
|
# The changed state is then set to +false+.
|
2003-02-03 16:52:21 +03:00
|
|
|
#
|
2011-05-17 01:53:12 +04:00
|
|
|
# <tt>*arg</tt>:: Any arguments to pass to the observers.
|
1998-01-16 15:13:05 +03:00
|
|
|
def notify_observers(*arg)
|
2000-04-10 09:48:43 +04:00
|
|
|
if defined? @observer_state and @observer_state
|
|
|
|
if defined? @observer_peers
|
2011-05-17 01:53:12 +04:00
|
|
|
@observer_peers.each do |k, v|
|
|
|
|
k.send v, *arg
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
@observer_state = false
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|
2003-02-03 16:52:21 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|