Observer pattern exemplified in Ruby

The observer design pattern is best demonstrated using the broadcaster <-> listener example.

A broadcaster in this example is an object having implementation for attaching listeners to itself. When the broadcaster will broadcast a new message, all the attached listeners are notified.

A listener class will register the broadcaster to itself so that it can access the message that has been broadcasted by the broadcaster. It will also have a method, which will be called by the broadcaster when notifying.

Let’s play around…