Skip to content

Instantly share code, notes, and snippets.

@andersonleite
Created October 25, 2009 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersonleite/218103 to your computer and use it in GitHub Desktop.
Save andersonleite/218103 to your computer and use it in GitHub Desktop.
Observer em Ruby
module Subject
def initialize
@observers=[]
end
def add_observer(&observer)
@observers << observer
end
def delete_observer(observer)
@observers.delete(observer)
end
def notify_observers
@observers.each do |observer|
observer.call(self)
end
end
end
class Funcionario
include Subject
attr_accessor :nome, :titulo, :salario
def initialize( nome, titulo, salario )
super()
@nome = nome
@titulo = titulo
@salario = salario
end
def salario=(novo_salario)
@salario = novo_salario
notify_observers
end
end
jose = Funcionario.new('jose', 'Crane Operator', 30000)
jose.add_observer do |funcionario|
puts ("Aumentando salário do #{funcionario.nome}!")
puts("Seu novo salário é #{funcionario.salario}!")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment