diff options
author | drbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-05-16 21:53:12 +0000 |
---|---|---|
committer | drbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-05-16 21:53:12 +0000 |
commit | 1234af557bfe184fbdd5d40076c41f0feee737de () | |
tree | 3b91b953f649478db2367573bbb50e7b32464a2d /lib/observer.rb | |
parent | 61a5a6393d398a509352ffed49e662595d652d1f (diff) |
* lib/observer.rb: Improve documentation. by David Copeland.
[Ruby 1.9 - Bug #4707] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31599 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | lib/observer.rb | 73 |
1 files changed, 41 insertions, 32 deletions
@@ -1,33 +1,34 @@ # -# observer.rb implements the _Observer_ object-oriented design pattern. The # following documentation is copied, with modifications, from "Programming # Ruby", by Hunt and Thomas; http://www.rubycentral.com/book/lib_patterns.html. # -# == About -# -# 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 # -# In the Ruby implementation, the notifying class mixes in the +Observable+ # module, which provides the methods for managing the associated observer # objects. # -# The observers must implement the +update+ method 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 +@symbol+. 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 @@ -108,19 +109,20 @@ # Current price: 112 # Current price: 79 # --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 79 - - -# -# Implements the Observable design pattern as a mixin so that other objects can -# be notified of changes in state. See observer.rb for details and an example. -# module Observable # - # Add +observer+ as an observer on this object. +observer+ will now receive - # notifications. The second optional argument specifies a method to notify - # updates, of which default value is +update+. # def add_observer(observer, func=:update) @observer_peers = {} unless defined? @observer_peers unless observer.respond_to? func @@ -130,15 +132,16 @@ module Observable end # - # Delete +observer+ as an observer on this object. It will no longer receive - # notifications. # def delete_observer(observer) @observer_peers.delete observer if defined? @observer_peers end # - # Delete all observers associated with this object. # def delete_observers @observer_peers.clear if defined? @observer_peers @@ -159,12 +162,15 @@ module Observable # Set the changed state of this object. Notifications will be sent only if # the changed +state+ is +true+. # def changed(state=true) @observer_state = state end # - # Query the changed state of this object. # def changed? if defined? @observer_state and @observer_state @@ -175,16 +181,19 @@ module Observable end # - # If this object's changed state is +true+, invoke the update method in each - # currently associated observer in turn, passing it the given arguments. The - # changed state is then set to +false+. # def notify_observers(*arg) if defined? @observer_state and @observer_state if defined? @observer_peers - @observer_peers.each { |k, v| - k.send v, *arg - } end @observer_state = false end |