GIF87a;
# # Implementation of the _Observer_ object-oriented design pattern. The # following documentation is copied, with modifications, from "Programming # Ruby", by Hunt and Thomas; http://www.ruby-doc.org/docs/ProgrammingRuby/html/lib_patterns.html. # # See Observable for more info. # The Observer pattern (also known as publish/subscribe) provides a simple # mechanism for one object to inform a set of interested third-party objects # when its state changes. # # == Mechanism # # The notifying class mixes in the +Observable+ # module, which provides the methods for managing the associated observer # objects. # # The observers must implement a method called +update+ to receive # notifications. # # The observable object must: # * assert that it has +#changed+ # * call +#notify_observers+ # # === Example # # The following example demonstrates this nicely. A +Ticker+, when run, # 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. # # 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" # # class Ticker ### Periodically fetch a stock price. # include Observable # # def initialize(symbol) # @symbol = symbol # end # # def run # lastPrice = nil # loop do # price = Price.fetch(@symbol) # print "Current price: #{price}\n" # if price != lastPrice # changed # notify observers # lastPrice = 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 Price.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 # # 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 module Observable # # Add +observer+ as an observer on this object. so that it will receive # notifications. # # +observer+:: the object that will be notified of changes. # +func+:: Symbol naming the method that will be called when this Observable # has changes. # # 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 def add_observer(observer, func=:update) @observer_peers = {} unless defined? @observer_peers unless observer.respond_to? func raise NoMethodError, "observer does not respond to `#{func.to_s}'" end @observer_peers[observer] = func end # # Remove +observer+ as an observer on this object so that it will no longer # receive notifications. # # +observer+:: An observer of this Observable def delete_observer(observer) @observer_peers.delete observer if defined? @observer_peers end # # Remove all observers associated with this object. # def delete_observers @observer_peers.clear if defined? @observer_peers end # # Return the number of observers associated with this object. # def count_observers if defined? @observer_peers @observer_peers.size else 0 end end # # Set the changed state of this object. Notifications will be sent only if # the changed +state+ is +true+. # # +state+:: Boolean indicating the changed state of this Observable. # def changed(state=true) @observer_state = state end # # Returns true if this object's state has been changed since the last # #notify_observers call. # def changed? if defined? @observer_state and @observer_state true else false end end # # Notify observers of a change in state *if* this object's changed state is # +true+. # # This will invoke the method named in #add_observer, passing <tt>*arg</tt>. # The changed state is then set to +false+. # # <tt>*arg</tt>:: Any arguments to pass to the observers. def notify_observers(*arg) if defined? @observer_state and @observer_state if defined? @observer_peers @observer_peers.each do |k, v| k.send v, *arg end end @observer_state = false end end end
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
bigdecimal | Folder | 0755 |
|
|
cgi | Folder | 0755 |
|
|
date | Folder | 0755 |
|
|
digest | Folder | 0755 |
|
|
dl | Folder | 0755 |
|
|
drb | Folder | 0755 |
|
|
fiddle | Folder | 0755 |
|
|
io | Folder | 0755 |
|
|
irb | Folder | 0755 |
|
|
json | Folder | 0755 |
|
|
matrix | Folder | 0755 |
|
|
net | Folder | 0755 |
|
|
openssl | Folder | 0755 |
|
|
optparse | Folder | 0755 |
|
|
psych | Folder | 0755 |
|
|
racc | Folder | 0755 |
|
|
rbconfig | Folder | 0755 |
|
|
rexml | Folder | 0755 |
|
|
rinda | Folder | 0755 |
|
|
ripper | Folder | 0755 |
|
|
rss | Folder | 0755 |
|
|
shell | Folder | 0755 |
|
|
syslog | Folder | 0755 |
|
|
test | Folder | 0755 |
|
|
uri | Folder | 0755 |
|
|
vendor_ruby | Folder | 0755 |
|
|
webrick | Folder | 0755 |
|
|
xmlrpc | Folder | 0755 |
|
|
yaml | Folder | 0755 |
|
|
English.rb | File | 6.44 KB | 0644 |
|
abbrev.rb | File | 3.31 KB | 0644 |
|
base64.rb | File | 2.63 KB | 0644 |
|
benchmark.rb | File | 17.94 KB | 0644 |
|
cgi.rb | File | 9.39 KB | 0644 |
|
cmath.rb | File | 7.22 KB | 0644 |
|
complex.rb | File | 380 B | 0644 |
|
csv.rb | File | 81.32 KB | 0644 |
|
date.rb | File | 946 B | 0644 |
|
debug.rb | File | 28.9 KB | 0644 |
|
delegate.rb | File | 9.78 KB | 0644 |
|
digest.rb | File | 2.24 KB | 0644 |
|
dl.rb | File | 280 B | 0644 |
|
drb.rb | File | 19 B | 0644 |
|
e2mmap.rb | File | 3.8 KB | 0644 |
|
erb.rb | File | 26.08 KB | 0644 |
|
expect.rb | File | 2.14 KB | 0644 |
|
fiddle.rb | File | 1.25 KB | 0644 |
|
fileutils.rb | File | 46.35 KB | 0644 |
|
find.rb | File | 2.08 KB | 0644 |
|
forwardable.rb | File | 7.56 KB | 0644 |
|
getoptlong.rb | File | 15.38 KB | 0644 |
|
gserver.rb | File | 8.86 KB | 0644 |
|
ipaddr.rb | File | 26.17 KB | 0644 |
|
irb.rb | File | 20.03 KB | 0644 |
|
json.rb | File | 1.74 KB | 0644 |
|
kconv.rb | File | 5.74 KB | 0644 |
|
logger.rb | File | 20.96 KB | 0644 |
|
mathn.rb | File | 6.52 KB | 0644 |
|
matrix.rb | File | 45.02 KB | 0644 |
|
mkmf.rb | File | 78.13 KB | 0644 |
|
monitor.rb | File | 6.93 KB | 0644 |
|
mutex_m.rb | File | 2 KB | 0644 |
|
observer.rb | File | 5.71 KB | 0644 |
|
open-uri.rb | File | 23.66 KB | 0644 |
|
open3.rb | File | 21.17 KB | 0644 |
|
openssl.rb | File | 528 B | 0644 |
|
optparse.rb | File | 51.27 KB | 0644 |
|
ostruct.rb | File | 7.64 KB | 0644 |
|
pathname.rb | File | 15.3 KB | 0644 |
|
pp.rb | File | 13.14 KB | 0644 |
|
prettyprint.rb | File | 9.63 KB | 0644 |
|
prime.rb | File | 13.98 KB | 0644 |
|
profile.rb | File | 205 B | 0644 |
|
profiler.rb | File | 4.29 KB | 0644 |
|
pstore.rb | File | 14.85 KB | 0644 |
|
psych.rb | File | 11.45 KB | 0644 |
|
rational.rb | File | 308 B | 0644 |
|
resolv-replace.rb | File | 1.73 KB | 0644 |
|
resolv.rb | File | 61.46 KB | 0644 |
|
ripper.rb | File | 2.53 KB | 0644 |
|
rss.rb | File | 2.84 KB | 0644 |
|
scanf.rb | File | 23.52 KB | 0644 |
|
securerandom.rb | File | 8.56 KB | 0644 |
|
set.rb | File | 17.32 KB | 0644 |
|
shell.rb | File | 10.3 KB | 0644 |
|
shellwords.rb | File | 5.94 KB | 0644 |
|
singleton.rb | File | 4.02 KB | 0644 |
|
socket.rb | File | 25.76 KB | 0644 |
|
sync.rb | File | 7.26 KB | 0644 |
|
tempfile.rb | File | 10.15 KB | 0644 |
|
thread.rb | File | 6.94 KB | 0644 |
|
thwait.rb | File | 3.38 KB | 0644 |
|
time.rb | File | 21.09 KB | 0644 |
|
timeout.rb | File | 3.16 KB | 0644 |
|
tmpdir.rb | File | 4.29 KB | 0644 |
|
tracer.rb | File | 6.54 KB | 0644 |
|
tsort.rb | File | 6.79 KB | 0644 |
|
un.rb | File | 8.34 KB | 0644 |
|
uri.rb | File | 3.07 KB | 0644 |
|
weakref.rb | File | 3.23 KB | 0644 |
|
webrick.rb | File | 6.7 KB | 0644 |
|
xmlrpc.rb | File | 8.49 KB | 0644 |
|
yaml.rb | File | 2.3 KB | 0644 |
|