Singleton: Difference between revisions
Content added Content deleted
({{Omit From|ALGOL 68}} <!-- it isn't immediately obvious that ALGOL 68 is object oriented -->) |
(→{{header|Tcl}}: Tighten, comment) |
||
Line 416: | Line 416: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{works with|Tcl|8.6}} |
{{works with|Tcl|8.6}} <br> |
||
{{works with|Tcl|8.5}}and the [http://wiki.tcl.tk/TclOO TclOO package] |
{{works with|Tcl|8.5}} and the [http://wiki.tcl.tk/TclOO TclOO package] |
||
ref http://wiki.tcl.tk/21595 |
ref http://wiki.tcl.tk/21595 |
||
<lang tcl>package require Tcl 8.6 |
<lang tcl>package require Tcl 8.6 |
||
# This is a metaclass, a class that defines the behavior of other classes |
|||
oo::class create singleton { |
oo::class create singleton { |
||
superclass oo::class |
superclass oo::class |
||
variable object |
variable object |
||
unexport create ;# Doesn't make sense to have named singletons |
|||
method create {name args} { |
|||
if {![info exists object]} { |
|||
set object [next $name {*}$args] |
|||
} |
|||
return $object |
|||
} |
|||
method new args { |
method new args { |
||
if {![info exists object]} { |
if {![info exists object]} { |
||
Line 439: | Line 435: | ||
} |
} |
||
singleton create example { |
|||
self mixin singleton |
|||
method counter {} { |
method counter {} { |
||
my variable count |
my variable count |