mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-08-28 05:25:04 +00:00
17 lines
582 B
Python
17 lines
582 B
Python
"""Module providing a decorator to implement the Singleton design pattern."""
|
|
|
|
|
|
def singleton(cls):
|
|
"""A class decorator that ensures only one instance of a class exists."""
|
|
|
|
# Dictionary to cache the single instance of the class
|
|
_instance = {}
|
|
|
|
def _singleton(*args, **kwargs):
|
|
"""Return the existing instance or create a new one if it doesn't exist."""
|
|
if cls not in _instance:
|
|
# Create and store the instance if it's the first call
|
|
_instance[cls] = cls(*args, **kwargs)
|
|
return _instance[cls]
|
|
|
|
return _singleton
|