2 # observer.rb implements the _Observer_ object-oriented design pattern. The
3 # following documentation is copied, with modifications, from "Programming
4 # Ruby", by Hunt and Thomas; http://www.rubycentral.com/book/lib_patterns.html.
8 # The Observer pattern, also known as Publish/Subscribe, provides a simple
9 # mechanism for one object to inform a set of interested third-party objects
10 # when its state changes.
14 # In the Ruby implementation, the notifying class mixes in the +Observable+
15 # module, which provides the methods for managing the associated observer
18 # The observers must implement the +update+ method to receive notifications.
20 # The observable object must:
21 # * assert that it has +changed+
22 # * call +notify_observers+
26 # The following example demonstrates this nicely. A +Ticker+, when run,
27 # continually receives the stock +Price+ for its +@symbol+. A +Warner+ is a
28 # general observer of the price, and two warners are demonstrated, a +WarnLow+
29 # and a +WarnHigh+, which print a warning if the price is below or above their
30 # set limits, respectively.
32 # The +update+ callback allows the warners to run without being explicitly
33 # called. The system is set up with the +Ticker+ and several observers, and the
34 # observers do their duty without the top-level code having to interfere.
36 # Note that the contract between publisher and subscriber (observable and
37 # observer) is not declared or enforced. The +Ticker+ publishes a time and a
38 # price, and the warners receive that. But if you don't ensure that your
39 # contracts are correct, nothing else can warn you.
43 # class Ticker ### Periodically fetch a stock price.
46 # def initialize(symbol)
53 # price = Price.fetch(@symbol)
54 # print "Current price: #{price}\n"
55 # if price != lastPrice
56 # changed # notify observers
58 # notify_observers(Time.now, price)
65 # class Price ### A mock class to fetch a stock price (60 - 140).
66 # def Price.fetch(symbol)
71 # class Warner ### An abstract observer of Ticker objects.
72 # def initialize(ticker, limit)
74 # ticker.add_observer(self)
78 # class WarnLow < Warner
79 # def update(time, price) # callback for observer
81 # print "--- #{time.to_s}: Price below #@limit: #{price}\n"
86 # class WarnHigh < Warner
87 # def update(time, price) # callback for observer
89 # print "+++ #{time.to_s}: Price above #@limit: #{price}\n"
94 # ticker = Ticker.new("MSFT")
95 # WarnLow.new(ticker, 80)
96 # WarnHigh.new(ticker, 120)
103 # --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 75
106 # +++ Sun Jun 09 00:10:25 CDT 2002: Price above 120: 134
110 # --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 79
114 # Implements the Observable design pattern as a mixin so that other objects can
115 # be notified of changes in state. See observer.rb for details and an example.
120 # Add +observer+ as an observer on this object. +observer+ will now receive
121 # notifications. The second optional argument specifies a method to notify
122 # updates, of which default value is +update+.
124 def add_observer(observer, func=:update)
125 @observer_peers = {} unless defined? @observer_peers
126 unless observer.respond_to? func
127 raise NoMethodError, "observer does not respond to `#{func.to_s}'"
129 @observer_peers[observer] = func
133 # Delete +observer+ as an observer on this object. It will no longer receive
136 def delete_observer(observer)
137 @observer_peers.delete observer if defined? @observer_peers
141 # Delete all observers associated with this object.
144 @observer_peers.clear if defined? @observer_peers
148 # Return the number of observers associated with this object.
151 if defined? @observer_peers
159 # Set the changed state of this object. Notifications will be sent only if
160 # the changed +state+ is +true+.
162 def changed(state=true)
163 @observer_state = state
167 # Query the changed state of this object.
170 if defined? @observer_state and @observer_state
178 # If this object's changed state is +true+, invoke the update method in each
179 # currently associated observer in turn, passing it the given arguments. The
180 # changed state is then set to +false+.
182 def notify_observers(*arg)
183 if defined? @observer_state and @observer_state
184 if defined? @observer_peers
185 @observer_peers.each { |k, v|
189 @observer_state = false