Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Borg pattern or just module with functions

I was thinking about using Singleton pattern in my project, so I searched StackOverflow to find a pythonic way to implement it. I found this question with the accepted answer saying that "a module with functions (and not a class) would serve well as a singleton". On other hand the second answer had a suggestion to use the Borg pattern. For me using a module is a simple and straightforward solution, so I'd like to understand when using Borg is preferable.

like image 375
legesh Avatar asked Jan 21 '23 01:01

legesh


1 Answers

A singleton and a module represent only one instance, for all the application lifetime. This instance is kept instantiated, even when it's not needed.

The borg pattern is about shared state. Each client class will create a new instance of the borg, but that instance will be disposed when it's not needed anymore - it is a much more elegant approach.

Beyond that, it is much easier to subclass or mock a borg.

like image 146
rsenna Avatar answered Feb 01 '23 06:02

rsenna