## Tcl9.0.3/Tk9.0.3 Documentation > Tcl Commands > singleton

### Tcl/Tk Applications | Tcl Commands | Tk Commands | [incr Tcl] Package Commands | SQLite3 Package Commands | TDBC Package Commands | tdbc::mysql Package Commands | tdbc::odbc Package Commands | tdbc::postgres Package Commands | tdbc::sqlite3 Package Commands | Thread Package Commands | Tcl C API | Tk C API | [incr Tcl] Package C API | TDBC Package C API

  NAME
    oo::singleton — それ自身の一つの具現値を許容するのみの級 A class that does only allows one instance of itself
  構文 SYNOPSIS
  級の階層構造 CLASS HIERARCHY
  説明 DESCRIPTION
  構成子 CONSTRUCTOR
  破壊子 DESTRUCTOR
  移送手段 EXPORTED METHODS
    cls new ?arg ...?
  非移送手段 NON-EXPORTED METHODS
  用例 EXAMPLE
  SEE ALSO
  KEYWORDS

### NAME

oo::singleton — それ自身の一つの具現値を許容するのみの級 A class that does only allows one instance of itself

### 構文 SYNOPSIS

package require tcl::oo
oo::singleton method ?arg ...?

### 級の階層構造 CLASS HIERARCHY

oo::object
→ oo::class
→ oo::singleton

### 説明 DESCRIPTION

singleton 級は､それ自身の具現値が高々一つだけ実在することを許容する級である｡それらは､create と createWithNamespace 手段を完全に非移送し､既存の具現値がない場合のみ新しい具現値を作成する様に new 手段を上書きする｡singletonの級から継承することは推奨されない; singleton-らしさ は継承されない｡singletonの級の構成子が何かの引数を採ることは推奨されない｡ 　 Singleton classes are classes that only permit at most one instance of themselves to exist. They unexport the create and createWithNamespace methods entirely, and override the new 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 not inherited. It is not recommended that a singleton class's constructor take any arguments.

具現値はその破壊手段を､対象の破壊を思いとどまらせる為に､常に不都合を返す手段によって上書きさせる､しかし､もし厳に必要なら､破壊の保留は可能である(譬えば､級の破壊や削除する為の rename の使用によって)｡それは､又､(非-移送の)<複製された>手段をその上に定義させる､即ち､ oo::copy の失敗を伴う singleton 具現値を使用することを試みさせる為に､同じ様に常に不都合を返す｡ Instances have their destroy 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 rename to delete it). They also have a (non-exported) <cloned> method defined on them that similarly always returns errors to make attempts to use the singleton instance with oo::copy fail.

#### 構成子 CONSTRUCTOR

oo::singleton は明示的な構成子を定義しない; これは oo::class 級の構成子と効果的に同じである｡ The oo::singleton class does not define an explicit constructor; this means that it is effectively the same as the constructor of the oo::class class.

#### 破壊子 DESTRUCTOR

oo::singleton 級は明示的な破壊子を定義しない; その具現値の破壊は通常の級の破壊に似ている(又､singleton 対象を破壊する事になる)｡ The oo::singleton 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 new ?arg ...?
  もし実在するなら､singleton 級の現在の具現値を返す､具現値が実在しない場合のみ､新しい具現値を創生する｡追加の引数､arg ..., are は､新しい具現値が実際生成される場合のみ使われる; その生成は oo::class 級の新しい手段経由である｡ 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, arg ..., are only used if a new instance is actually manufactured; that construction is via the oo::class class's new method.

  これは､上位級の手段を､上位級の実装への signature群 への同一の呼出しによる振舞の上書きである｡(訳註: signature 函数や手段の名前､引数の数､データ型､返値の型等の組合せ) 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 oo::singleton class explicitly states that create and createWithNamespace are unexported; callers should not assume that they have control over either the name or the namespace name of the singleton instance.

### 用例 EXAMPLE

この例では､具現値は一つのみであるが､new 手段は三回呼出される｡ This example demonstrates that there is only one instance even though the new 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"
    }

註意すべきは､singletonの具現値の名前は特に何も保障しない｡ Note that the name of the instance of the singleton is not guaranteed to be anything in particular.

### SEE ALSO

oo::class

### KEYWORDS

class, metaclass, object, single instance
Copyright © 2018 Donal K. Fellows