Singleton: Difference between revisions
Content added Content deleted
m (→Icon and Unicon: header simplification) |
(Logtalk example) |
||
Line 624: | Line 624: | ||
// Any other methods |
// Any other methods |
||
}</lang> |
}</lang> |
||
=={{header|Logtalk}}== |
|||
Logtalk supports both classes and prototypes. A prototype is a much simpler solution for defining a singleton object than defining a class with only an instance. |
|||
<lang logtalk> |
|||
:- object(singleton). |
|||
:- public(value/1). |
|||
value(Value) :- |
|||
state(Value). |
|||
:- public(set_value/1). |
|||
set_value(Value) :- |
|||
retract(state(_)), |
|||
assertz(state(Value)). |
|||
:- private(state/1). |
|||
:- dynamic(state/1). |
|||
state(0). |
|||
:- end_object. |
|||
</lang> |
|||
A simple usage example after compiling and loading the code above: |
|||
<lang logtalk> |
|||
| ?- singleton::value(Value). |
|||
Value = 0 |
|||
yes |
|||
| ?- singleton::(set_value(1), value(Value)). |
|||
Value = 1 |
|||
yes |
|||
</lang> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |