[ANN] observable

Hello, “observable.rb” is a variation on the observer pattern. It allows
multiple observers to register interest in the value of a variable, and
be notified when it changes. The main differences with Observer
(observer.rb) are that it is per-variable, not per-object, that it is
based on closures, and that variable state can be matched against
several patterns or values, each with a corresponding action.

Source, examples, and docs are at:

http://redshift.sourceforge.net/observable/

A simple example:

 class Window
   observable :visible # true or false
 end

 class WindowVisibleMenuCmd
   def initialize window
     window.when_visible true do
       @checked = true
     end
     window.when_visible false do
       @checked = false
     end
     @window = window
   end

   def do_command
     @window.visible = !@checked
   end

   def inspect
     "menu is #{"not " if not @checked}checked" +
     " and window is #{"not " if not @window.visible}visible"
   end
 end

 window = Window.new
 window.visible = true

 menu_cmd = WindowVisibleMenuCmd.new window

 puts "Before command, " + menu_cmd.inspect
 menu_cmd.do_command
 puts "After command, " + menu_cmd.inspect

 # Output:
 # Before command, menu is checked and window is visible
 # After command, menu is not checked and window is not visible

You know the “goto” statement? People didn’t like it. It made trying to
work out how you got there a bit difficult.

Well the observer pattern is a bit like a “come-from” statement.

I can assure you from personal experience that in a big system it can lead
to some very elusive and weird bugs. Especially when a notify can trigger
notifies one or more of which can trigger notifies one or more of which…

Man! Your the flow of control logic can get very tangled!

Take care and good luck!

···

On Mon, 11 Nov 2002, Joel VanderWerf wrote:

Hello, “observable.rb” is a variation on the observer pattern.

John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : john.carter@tait.co.nz
New Zealand

Good Ideas:
Ruby - http://www.ruby-lang-org - The best of perl,python,scheme without the pain.
Valgrind - Valgrind Home - memory debugger for x86-GNU/Linux
Free your books - http://www.bookcrossing.com