Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
21
Task/Singleton/Tcl/singleton-1.tcl
Normal file
21
Task/Singleton/Tcl/singleton-1.tcl
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package require TclOO
|
||||
|
||||
# This is a metaclass, a class that defines the behavior of other classes
|
||||
oo::class create singleton {
|
||||
superclass oo::class
|
||||
variable object
|
||||
unexport create ;# Doesn't make sense to have named singletons
|
||||
method new args {
|
||||
if {![info exists object]} {
|
||||
set object [next {*}$args]
|
||||
}
|
||||
return $object
|
||||
}
|
||||
}
|
||||
|
||||
singleton create example {
|
||||
method counter {} {
|
||||
my variable count
|
||||
return [incr count]
|
||||
}
|
||||
}
|
||||
14
Task/Singleton/Tcl/singleton-2.tcl
Normal file
14
Task/Singleton/Tcl/singleton-2.tcl
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
% set a [example new]
|
||||
::oo::Obj20
|
||||
% set b [example new] ;# note how this returns the same object name
|
||||
::oo::Obj20
|
||||
% expr {$a == $b}
|
||||
1
|
||||
% $a counter
|
||||
1
|
||||
% $b counter
|
||||
2
|
||||
% $a counter
|
||||
3
|
||||
% $b counter
|
||||
4
|
||||
Loading…
Add table
Add a link
Reference in a new issue