The Borg pattern allows multiple instances of a class to share the same internal state. Unlike the Singleton pattern, which restricts you to one instance, Borg lets you create many instances that all behave as if they share one “brain.”
Sometimes, you need multiple objects to share the same data—like configuration settings or a shared resource pool. While singletons or global variables can do this, they often reduce flexibility and create tight coupling. The Borg pattern offers a cleaner way: multiple independent objects that all share the same state.
Use the Borg pattern when:
Avoid this pattern if:
The Borg pattern works by setting each instance’s __dict__
(which holds its attributes) to point to a shared dictionary defined at the class level. Any change made to one instance’s attributes is immediately reflected in all others.
Think of several scientists using the same whiteboard. Each person can write on it, and everyone sees the changes instantly. They’re working independently, but sharing the same workspace.
class Borg:
_shared_state = {}
def __init__(self):
self.__dict__ = self._shared_state
# All Borg instances share the same state
b1 = Borg()
b2 = Borg()
b1.x = 10 # Set x using one instance
print(b2.x) # The other instance sees the same value
In this example, both b1
and b2
are separate objects, but they share the same attributes and data.
Explore the complete implementation here: Borg Pattern on GitHub