ruby中的观察者模式

 

require 'observer'

class TV
include Observable

def turn_on
puts "TV is on!"
changed #TV state changed
notify_observers(self) #notify observer
end
end

class Me
def update(tv) #observer, when someone notify you
puts "I will go to watching TV, #{tv.inspect}"
end
end

tv = TV.new
me = Me.new

tv.add_observer(me)

tv.turn_on

你可能感兴趣的:(Ruby,Go)