7.2. Borg
EN: Borg
PL: Borg
Type: object
The Borg design pattern, also known as the Monostate pattern, is a design pattern that ensures all instances of a class share the same state. In Python, this is typically implemented using a shared dictionary.
Here's a simple example of the Borg pattern in Python:
In the following example, Borg is a class that has a class attribute state which is a dictionary. This dictionary is assigned to the instance dictionary in the __init__ method. Therefore, when an attribute is set in one instance, it is available in all instances.
>>> class Borg:
... state = {}
...
... def __init__(self):
... self.__dict__ = self.state
Usage:
>>> a = Borg()
>>> b = Borg()
>>>
>>> a.data = 1
>>>
>>>
>>> print(a.data)
1
>>> print(b.data)
1
Subclass is a subclass of Borg and it shares the same state as its parent class.
>>> class Subclass(Borg):
... pass
>>>
>>> x = Subclass()
>>>
>>> print(x.data)
1
The real reason that borg is different comes down to subclassing. If you subclass a borg, the subclass' objects have the same state as their parents classes objects, unless you explicitly override the shared state in that subclass. Each subclass of the singleton pattern has its own state and therefore will produce different objects. Also in the singleton pattern the objects are actually the same, not just the state (even though the state is the only thing that really matters). [1]
7.2.1. Pattern
7.2.2. Problem
>>> class Config:
... pass
>>>
>>>
>>> conf1 = Config()
>>> conf2 = Config()
>>> conf1
<__main__.Config object at 0x110228590>
>>>
>>> conf2
<__main__.Config object at 0x1076f2c10>
>>> conf1.host = '127.0.0.1'
>>> conf1.port = 5432
>>> conf1.username = 'mwatney'
>>> conf1.password = 'Ares3'
>>> conf1.schema = 'nasa'
>>> vars(conf1)
{'host': '127.0.0.1',
'port': 5432,
'username': 'mwatney',
'password': 'Ares3',
'schema': 'nasa'}
>>>
>>> vars(conf2)
{}
>>> conf1.host
'127.0.0.1'
>>>
>>> conf2.host
Traceback (most recent call last):
AttributeError: 'Config' object has no attribute 'host'
7.2.3. Solution
>>> class Borg:
... _state = {}
...
... def __init__(self):
... self.__dict__ = self._state
>>>
>>>
>>> class Config(Borg):
... pass
>>>
>>>
>>> conf1 = Config()
>>> conf2 = Config()
>>>
>>>
>>> conf1
<__main__.Config object at 0x110228590>
>>>
>>> conf2
<__main__.Config object at 0x1076f2c10>
>>>
>>>
>>> conf1.host = '127.0.0.1'
>>> conf1.port = 5432
>>> conf1.username = 'mwatney'
>>> conf1.password = 'Ares3'
>>> conf1.schema = 'nasa'
>>> vars(conf1)
{'host': '127.0.0.1',
'port': 5432,
'username': 'mwatney',
'password': 'Ares3',
'schema': 'nasa'}
>>>
>>> vars(conf2)
{'host': '127.0.0.1',
'port': 5432,
'username': 'mwatney',
'password': 'Ares3',
'schema': 'nasa'}
>>> conf1.host
'127.0.0.1'
>>>
>>> conf2.host
'127.0.0.1'
class Borg:
shared_state: dict = {}
def __init__(self):
self.__dict__ = self.shared_state
7.2.4. References
7.2.5. Assignments
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: DesignPatterns Creational Borg
# - Difficulty: easy
# - Lines: 4
# - Minutes: 3
# %% English
# 1. Implement Borg pattern
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zaimplementuj wzorzec Borg
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> a = Borg()
>>> b = Borg()
>>> a is b
False
>>> a.name = 'Mark'
>>> b.name
'Mark'
"""
class Borg:
pass