Singleton Factory API
sweet_tea.singleton_factory.SingletonFactory
Bases: BaseFactory
Factory for registering and retrieving singleton instances.
This implements the service locator pattern where pre-configured instances can be registered once and retrieved multiple times. This is useful for:
- Singleton services that should only exist once
- Pre-configured database connections
- Dependency injection containers
- Caching expensive-to-create objects
Thread-safe: All operations are synchronized to prevent race conditions.
Source code in sweet_tea/singleton_factory.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
clear()
classmethod
Remove all registered instances.
This is primarily useful for testing or resetting the factory state.
Source code in sweet_tea/singleton_factory.py
create(key, library='', label='', configuration=None)
classmethod
Get an existing singleton instance or create a new one if it doesn't exist.
This method provides lazy initialization - instances are created only when first requested. Subsequent calls with the same key will return the same cached instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Name to reference the class from the registry. |
required |
library
|
str
|
Optional library filter for the class. |
''
|
label
|
str
|
Optional label filter for the class. |
''
|
configuration
|
dict[str, Any] | None
|
Configuration parameters as keyword arguments. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The existing singleton instance, or a newly created and registered instance. |
Raises:
| Type | Description |
|---|---|
SweetTeaError
|
If the key is not found in the registry or filters don't match. |
Source code in sweet_tea/singleton_factory.py
list_keys()
classmethod
Get a list of all class keys that can be created.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of available class keys from the registry in alphabetical order. |
Source code in sweet_tea/singleton_factory.py
list_singletons()
classmethod
Get a list of all cached singleton instance keys.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of registered singleton keys in alphabetical order. |
Source code in sweet_tea/singleton_factory.py
pop(key)
classmethod
Remove and return a registered instance.
This removes the instance and all its key variations from the registry, ensuring complete cleanup of the singleton.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key of the instance to remove. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The removed instance. |
Raises:
| Type | Description |
|---|---|
SweetTeaError
|
If no instance is registered for the given key. |
Source code in sweet_tea/singleton_factory.py
Methods
create()
sweet_tea.singleton_factory.SingletonFactory.create(key, library='', label='', configuration=None)
classmethod
Get an existing singleton instance or create a new one if it doesn't exist.
This method provides lazy initialization - instances are created only when first requested. Subsequent calls with the same key will return the same cached instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Name to reference the class from the registry. |
required |
library
|
str
|
Optional library filter for the class. |
''
|
label
|
str
|
Optional label filter for the class. |
''
|
configuration
|
dict[str, Any] | None
|
Configuration parameters as keyword arguments. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The existing singleton instance, or a newly created and registered instance. |
Raises:
| Type | Description |
|---|---|
SweetTeaError
|
If the key is not found in the registry or filters don't match. |
Source code in sweet_tea/singleton_factory.py
clear()
sweet_tea.singleton_factory.SingletonFactory.clear()
classmethod
Remove all registered instances.
This is primarily useful for testing or resetting the factory state.
Source code in sweet_tea/singleton_factory.py
pop()
sweet_tea.singleton_factory.SingletonFactory.pop(key)
classmethod
Remove and return a registered instance.
This removes the instance and all its key variations from the registry, ensuring complete cleanup of the singleton.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key of the instance to remove. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The removed instance. |
Raises:
| Type | Description |
|---|---|
SweetTeaError
|
If no instance is registered for the given key. |
Source code in sweet_tea/singleton_factory.py
list_keys()
sweet_tea.singleton_factory.SingletonFactory.list_keys()
classmethod
Get a list of all class keys that can be created.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of available class keys from the registry in alphabetical order. |
Source code in sweet_tea/singleton_factory.py
list_singletons()
sweet_tea.singleton_factory.SingletonFactory.list_singletons()
classmethod
Get a list of all cached singleton instance keys.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of registered singleton keys in alphabetical order. |