Events/Observer pattern

Chris Double <>
Thu Nov 24 21:01:49 CET 2005


Thanks everyone, for the comments. In the end I went for writing
something quickly myself. The form of it is:

notifier(Observers) ->
    receive
        {notify, Message} ->
            lists:foreach(fun(To) ->
                            To ! Message
                          end,
                          Observers),
            notifier(Observers);
        {register, Observer} ->
            notifier([Observer|Observers]);
        {unregister, Observer} ->
            notifier(lists:delete(Observer, Observers))
    end.

Which turns out to be quite easy. The above doesn't take into
registered processes that die before unregistering of course but
that's easy to handle.

Matthias was right in that it was so trivial to write in Erlang that
it was easier just to do than to work out how to use something else.
I'm still interested in what's out there though as once I'm familiar
with what other libraries do they'll be easier to reuse and presumably
provide more functionality.

Chris.
--
http://www.bluishcoder.co.nz



More information about the erlang-questions mailing list