在 SNIT 中要怎麼實作 Singleton pattern 呢?可以參考這段程式:
package require snit
snit::type Singleton {
typevariable _instance {}
typeconstructor {
set _instance [Singleton %AUTO%]
}
constructor {args} {
if {$_instance != {}} {
error "Singleton can only be accessed through \"getInstance\""
}
}
typemethod getInstance {} {
return $_instance
}
}