hbutils.design.singleton
- Overview:
Implementation of singleton design pattern.
This module provides metaclasses and utilities for implementing singleton patterns in Python. It includes:
SingletonMeta: A metaclass for creating traditional singleton classesValueBasedSingletonMeta: A metaclass for creating value-based singleton classesSingletonMark: A utility class for creating unique singleton marker objects
SingletonMeta
- class hbutils.design.singleton.SingletonMeta[source]
Meta class for singleton mode.
This metaclass ensures that only one instance of a class is created throughout the application’s lifetime. Subsequent calls to the class constructor will return the same instance.
- Example::
>>> class MyService(metaclass=SingletonMeta): >>> def get_value(self): >>> return 233 >>> >>> s = MyService() >>> s.get_value() # 233 >>> s1 = MyService() >>> s1 is s # True
Note
In native singleton pattern, the constructor is not needed because only one instance will be created in the whole lifetime. So when
SingletonMetais used as metaclass, please keep the constructor be non-argument, or just ignore the__init__function.
ValueBasedSingletonMeta
- class hbutils.design.singleton.ValueBasedSingletonMeta[source]
Meta class for value based singleton mode.
This metaclass creates singleton instances based on a value parameter. Multiple calls with the same value will return the same instance, while different values will create different instances.
- Example::
>>> class MyData(metaclass=ValueBasedSingletonMeta): >>> def __init__(self, value): >>> self.__value = value >>> >>> @property >>> def value(self): >>> return self.__value >>> >>> d1 = MyData(1) >>> d1.value # 1 >>> d2 = MyData(1) >>> d3 = MyData(2) >>> d2 is d1 # True >>> d2 is d3 # False
Note
This is an external case of singleton pattern. It can only contain one argument (must be positional-supported), which differs from the native singleton case.
SingletonMark
- class hbutils.design.singleton.SingletonMark(value)[source]
Singleton mark for some situation.
Can be used when some default value is needed, especially when None has meaning which is not default. This class creates unique marker objects that can be used as sentinel values.
- Example::
>>> NO_VALUE = SingletonMark("no_value") >>> NO_VALUE is SingletonMark("no_value") # True
Note
SingletonMarkis a value-based singleton class, can be used to create an unique value, especially in the cases whichNoneis not suitable for the default value.- __eq__(other) bool[source]
Compare equality between singleton marks.
SingletonMarkobjects can be directly compared with==.- Parameters:
other (Any) – The object to compare with.
- Returns:
True if marks are equal, False otherwise.
- Return type:
bool
- Examples::
>>> mark1 = SingletonMark('mark1') >>> mark1x = SingletonMark('mark1') >>> mark2 = SingletonMark('mark2') >>> mark1 == mark1 True >>> mark1 == mark1x True >>> mark1 == mark2 False
- __hash__() int[source]
Compute hash value for the singleton mark.
SingletonMarkobjects are hash supported and can be directly used indictandset.- Returns:
Hash value of the mark.
- Return type:
int
- __init__(mark: str)[source]
Constructor of
SingletonMark, can create a singleton mark object.- Parameters:
mark (str) – The string identifier for this mark.
- __repr__() str[source]
Return string representation of the singleton mark.
When you try to print a
SingletonMarkobject, its mark content will be displayed.- Returns:
String representation of the mark.
- Return type:
str
- Examples::
>>> mark1 = SingletonMark('mark1') >>> print(mark1) <SingletonMark 'mark1'>
- property mark: str
Get mark string of this mark object.
- Returns:
Mark string identifier.
- Return type:
str