Skip to content
Tcl Source Code Artifact [b012336cd3]
Login

Artifact [b012336cd3]

Artifact b012336cd383425812ebc3f846442c3e7ed9bb2bd961e52b65cddd3af21b4ca5:



CommandName: singleton ManualSection: n Version: 0.3 TclPart: TclOO TclDescription: TclOO Commands Links:

Name

oo::singleton - a class that does only allows one instance of itself

Synopsis

::: {.synopsis} ::: [package]{.cmd} [require]{.sub} [tcl::oo]{.lit}

[oo::singleton]{.cmd} [method]{.arg} [arg]{.optdot} :::

Class hierarchy

oo::object (-> fBoo::classfR (-> fBoo::singletonfR

Description

Singleton classes are classes that only permit at most one instance of themselves to exist. They unexport the fBcreatefR and fBcreateWithNamespacefR methods entirely, and override the fBnewfR method so that it only makes a new instance if there is no existing instance. It is not recommended to inherit from a singleton class; singleton-ness is fInotfR inherited. It is not recommended that a singleton class's constructor take any arguments.

Instances have theirfB destroyfR method overridden with a method that always returns an error in order to discourage destruction of the object, but destruction remains possible if strictly necessary (e.g., by destroying the class or using fBrenamefR to delete it). They also have a (non-exported) fB<cloned>fR method defined on them that similarly always returns errors to make attempts to use the singleton instance with fBoo::copyfR fail.

Constructor

The fBoo::singletonfR class does not define an explicit constructor; this means that it is effectively the same as the constructor of the fBoo::classfR class.

Destructor

The fBoo::singletonfR class does not define an explicit destructor; destroying an instance of it is just like destroying an ordinary class (and will destroy the singleton object).

Exported methods

cls fBnewfR ?fIarg ...fR? : This returns the current instance of the singleton class, if one exists, and creates a new instance only if there is no existing instance. The additional arguments, fIarg ...fR, are only used if a new instance is actually manufactured; that construction is via the fBoo::classfR class's fBnewfR method. This is an override of the behaviour of a superclass's method with an identical call signature to the superclass's implementation.

Non-exported methods

The fBoo::singletonfR class explicitly states that fBcreatefR and fBcreateWithNamespacefR are unexported; callers should not assume that they have control over either the name or the namespace name of the singleton instance.

Example

This example demonstrates that there is only one instance even though the fBnewfR method is called three times.

oo::singleton create Highlander {
    method say {} {
        puts "there can be only one"
    }
}

set h1 [Highlander new]
set h2 [Highlander new]
if {$h1 eq $h2} {
    puts "equal objects"    \(-> prints "equal objects"
}
set h3 [Highlander new]
if {$h1 eq $h3} {
    puts "equal objects"    \(-> prints "equal objects"
}

Note that the name of the instance of the singleton is not guaranteed to be anything in particular.