mirror of
https://github.com/rjNemo/design-patterns
synced 2026-06-06 02:26:40 +00:00
38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
class SingletonMeta(type):
|
|
"""
|
|
The Singleton class can be implemented in different ways in Python. Some
|
|
possible methods include: base class, decorator, metaclass. We will use the
|
|
metaclass because it is best suited for this purpose.
|
|
"""
|
|
|
|
_instances = {}
|
|
|
|
def __call__(cls, *args, **kwargs):
|
|
"""
|
|
Possible changes to the value of the `__init__` argument do not affect
|
|
the returned instance.
|
|
"""
|
|
if cls not in cls._instances:
|
|
instance = super().__call__(*args, **kwargs)
|
|
cls._instances[cls] = instance
|
|
return cls._instances[cls]
|
|
|
|
|
|
class Singleton(metaclass=SingletonMeta):
|
|
def some_business_logic(self):
|
|
"""
|
|
Finally, any singleton should define some business logic, which can be
|
|
executed on its instance.
|
|
"""
|
|
|
|
# ...
|
|
|
|
|
|
if __name__ == "__main__":
|
|
s1 = Singleton()
|
|
s2 = Singleton()
|
|
|
|
if id(s1) == id(s2):
|
|
print("Singleton works, both variables contain the same instance.")
|
|
else:
|
|
print("Singleton failed, variables contain different instances.")
|