Observers vs. Callbacks

callback, observer-pattern, ruby-on-rails

Solution

A callback is more short lived: You pass it into a function to be called once. It's part of the API in that you usually can't call the function without also passing a callback. This concept is tightly coupled with what the function does. Usually, you can only pass a single callback..

Example: Running a thread and giving a callback that is called when the thread terminates.

An observer lives longer and it can be attached/detached at any time. There can be many observers for the same thing and they can have different lifetimes.

Example: Showing values from a model in a UI and updating the model from user input.

Problem

i thought about using observers or callbacks. What and when you should use an observer? F.e. you could do following: ``` # User-model class User << AR after_create :send_greeting! def send_greeting! UserNotifier.deliver_greeting_message(self) end end #observer class UserNotifier << AR def greeting_message(user) ... end end ``` or you could create an observer and let it watch when users becomes created... What dou you recommened?

Original source