The Borg Pattern

Learn about the Borg pattern, which is a monostate pattern.

The solutions available through shared state should work for most cases, but if we really have to go for a singleton (and this has to be a really good exception), then there is one last alternative to it, only this is a riskier one to use.

Working with the Borg Pattern

This is the actual monostate pattern, referred to as the Borg pattern in Python. The idea is to create an object that is capable of replicating all of its attributes among all instances of the same class. The fact that absolutely every attribute is being replicated has to be a warning to keep in mind undesired side effects. Still, this pattern has many advantages over the singleton.

Example of the Borg pattern

In this case, we are going to split the previous object into two—one that works over Git tags, and the other over branches. And we are using the code that will make the Borg pattern work:

Get hands-on with 1200+ tech skills courses.