Singleton: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Changed to Wren S/H) |
(Add Ecstasy example) |
||
Line 839: | Line 839: | ||
# ... |
# ... |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Ecstasy}}== |
|||
The <code>static</code> keyword in a class declaration will compile that class as a singleton. It is legal to define <code>const</code> (i.e. immutable) and <code>service</code> classes as singletons. Modules, packages, and enumeration values are always singleton classes. It is <b>not</b> legal to define normal <code>class</code> classes as singletons, because normal classes are mutable, and Ecstasy does not allow shared mutable state. |
|||
The name of the class is used to specify that singleton instance: |
|||
<syntaxhighlight lang="ecstasy"> |
|||
module test { |
|||
static service Singleton { |
|||
private Int counter; |
|||
String fooHasBeenCalled() { |
|||
return $"{++counter} times"; |
|||
} |
|||
} |
|||
void run() { |
|||
@Inject Console console; |
|||
for (Int i : 1..5) { |
|||
console.print($"{Singleton.fooHasBeenCalled()=}"); |
|||
} |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
x$ xec test |
|||
Singleton.fooHasBeenCalled()=1 times |
|||
Singleton.fooHasBeenCalled()=2 times |
|||
Singleton.fooHasBeenCalled()=3 times |
|||
Singleton.fooHasBeenCalled()=4 times |
|||
Singleton.fooHasBeenCalled()=5 times |
|||
</pre> |
|||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |