---
name: "UR::Singleton"
section: "3pm"
platform: "linux"
package: "libur-perl"
version: "0.470+ds-3"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/UR::Singleton"
---

# UR::Singleton(3pm)

Abstract class for implementing singleton objects

## NAME

UR::Singleton - Abstract class for implementing singleton objects

## SYNOPSIS

```
  package MyApp::SomeClass;
  use UR;
  class MyApp::SomeClass {
      is => 'UR::Singleton',
      has => [
          foo => { is => 'Number' },
      ]
  };
  $obj = MyApp::SomeClass->get();
  $obj->foo(1);
```

## DESCRIPTION

This class provides the infrastructure for singleton classes.  Singletons are classes of which there can only be one instance, and that instance's ID is the class name.

If a class inherits from UR::Singleton, it overrides the default implementation of `get()` and `is_loaded()` in UR::Object with code that fabricates an appropriate object the first time it's needed.

Singletons are most often used as one of the parent classes for data sources within a Namespace.  This makes it convienent to refer to them using only their name, as in a class definition.

## METHODS

- \_singleton\_object

  ```
    $obj = Class::Name->_singleton_object;
    $obj = $obj->_singleton_object;
  ```

  Returns the object instance whether it is called as a class or object method.

- \_singleton\_class\_name

  ```
    $class_name = Class::Name->_singleton_class_name;
    $class_name = $obj->_singleton_class_name;
  ```

  Returns the class name whether it is called as a class or object method.

## SEE ALSO

UR::Object
