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
123 def add_observer(observer)
124 @observer_peers = [] unless defined? @observer_peers
125 unless observer.respond_to? :update
126 raise NoMethodError, "observer needs to respond to `update'"
128 @observer_peers.push observer
132 # Delete +observer+ as an observer on this object. It will no longer receive
135 def delete_observer(observer)
136 @observer_peers.delete observer if defined? @observer_peers
140 # Delete all observers associated with this object.
143 @observer_peers.clear if defined? @observer_peers
147 # Return the number of observers associated with this object.
150 if defined? @observer_peers
158 # Set the changed state of this object. Notifications will be sent only if
159 # the changed +state+ is +true+.
161 def changed(state=true)
162 @observer_state = state
166 # Query the changed state of this object.
169 if defined? @observer_state and @observer_state
177 # If this object's changed state is +true+, invoke the update method in each
178 # currently associated observer in turn, passing it the given arguments. The
179 # changed state is then set to +false+.
181 def notify_observers(*arg)
182 if defined? @observer_state and @observer_state
183 if defined? @observer_peers
184 for i in @observer_peers.dup
188 @observer_state = false