Singleton: Difference between revisions

1,729 bytes added ,  13 years ago
no edit summary
(Added PicoLisp)
No edit summary
Line 270:
// And any other protected methods.
}</lang>
 
=={{header|Erlang}}==
Erlang is not object-oriented, so there is no such thing as a singleton class. If you want something akin to a singleton, you start and register a process that maintains its state in a message loop and provides its state to anyone that wants it or needs to change it. Since this is done with message passing, it's safe for concurrent use.
 
<lang Erlang>
-module(singleton).
 
-export([get/1, set/2]).
 
-export([loop/1]).
 
% spec singleton:get(Key::any()) -> {ok, Value::any()} | {error, not_found} | timeout
get(Key) ->
Pid = singleton(),
Pid ! {get, self(), Key},
receive
{ok, Value} -> {ok, Value};
error -> {error, not_found};
Other -> exit(bad_message, Other)
after 2000 -> timeout
end.
 
% spec singleton:set(Key::any(), Value::any()) -> ok | timeout
set(Key, Value) ->
Pid = singleton(),
Pid ! {set, self(), Key, Value},
receive
ok -> ok;
Other -> exit(bad_message, Other)
after 2000 -> timeout
end.
 
singleton() ->
case whereis(singleton) of
undefined ->
register(singleton, spawn(?MODULE, loop, [dict:new()])),
singleton();
Pid -> Pid
end.
 
loop(Dict) ->
receive
{get, From, Key} ->
From ! dict:find(Key, Dict),
loop(Dict);
{set, From, Key, Value} ->
NewDict = dict:store(Key, Value, Dict),
From ! ok,
loop(NewDict);
_Other -> exit(bad_message)
end.
</lang>
 
Here is an example of how to use it (from the shell).
 
<lang Erlang>
1> singleton:get(apple).
{error,not_found}
2> singleton:set(apple, fruit).
ok
3> singleton:get(apple).
{ok,fruit}
4> singleton:set(apple, vegetable).
ok
5> singleton:get(apple).
{ok,vegetable}
6> singleton:set(tomato, vegetable).
ok
7> singleton:get(tomato).
{ok,vegetable}
</lang>
 
=={{header|Common Lisp}}==
Anonymous user