Singleton

Description

Make sure a class has one and only one instance that can be accessed globally.
More info

Profile

UML profile of design pattern

Profile download (Rational Rose model)

OCL Constraints

context Singleton
inv: self.feature->exists (b | b.oclIsKindOf (Operation) and
b.name = self.name and b.visibility <> VisibilityKind::public)

context InstanceVar
inv: self.ownerScope = ScopeKind::classifier

inv: self.type.name = self.singleton.name
	

Explanation of Profile

  1. A singleton class has an instance variable and operations that return a new instance or an existing instance.
  2. The instance variable must have classifier scope (i.e., static in languages like C++ and Java).
  3. The type of the instance variable must be the class itself.
  4. The singleton must have a protected or private constructor (i.e., an operation of the same name as the class itself).

Correct Instance

correct design pattern instance

Incorrect Instance

incorrect design pattern instance

The NameFactory class does not have a protected or private constructor. Its instance variable is also not static and its type is incorrect.

Tool Output

output of tool