views:

1229

answers:

4

Why is the Borg pattern better than the Singleton pattern?

I ask because I don't see them resulting in anything different.

Borg:

class Borg:
  __shared_state = {}
  # init internal state variables here
  __register = {}
  def __init__(self):
    self.__dict__ = self.__shared_state
    if not self.__register:
      self._init_default_register()

Singleton:

class Singleton:
  def __init__(self):
    # init internal state variables here
    self.__register = {}
    self._init_default_register()

# singleton mechanics external to class, for example this in the module
Singleton = Singleton()

What I want to display here is that the service object, whether implemented as Borg or Singleton, has a nontrivial internal state (it provides some service based on it) (I mean it has to be something useful it's not a Singleton/Borg just for fun).

And this state has to be inited. Here the Singleton implementation is more straightforward, since we treat init as the set-up of the global state. I find it awkward that the Borg object has to query its internal state to see if it should update itself.

It becomes worse the more internal state you have. For example, if the object has to listen to the Application's teardown signal to save its register to disk, that registration should only be done once as well, and this is easier with a Singleton.

+3  A: 

It is not. What is generally not recommended is a pattern like this in python:

class Singleton(object):

 _instance = None

 def __init__(self, ...):
  ...

 @classmethod
 def instance(cls):
  if cls._instance is None:
   cls._instance = cls(...)
  return cls._instance

where you use a class method to get the instance instead of the constructor. Python's metaprogramming allows much better methods, e.g. the one one wikipedia: http://en.wikipedia.org/wiki/Singleton_pattern#Python.

bayer
+4  A: 

A class basically describes how you can access (read/write) the internal state of your object.

In the singleton pattern you can only have a single class, i.e. all your objects will give you the same access points to the shared state. This means that if you have to provide an extended API, you will need to write a wrapper, wrapping around the singleton

In the borg pattern you are able to extend the base "borg" class, and thereby more conveniently extend the API for your taste.

Zed
+11  A: 

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).

Alex Martelli explains it best in this video. Watch at 23:17 in it.

David Raznick
> 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).Why is that a bad thing?
uswaretech
good question uswaretech, it is a part of my question above. What is that said as bad?
kaizer.se
thank you for the video link
kaizer.se
I did not say it was a bad thing. It was an opinion-less observation on the differences. Sorry for the confusion. Sometimes the singleton will be better infact, if for example you do any checks on the objects id by id(obj), even though this is rare.
David Raznick
+3  A: 

It's only better in those few cases where you actually have a difference. Like when you subclass. The Borg pattern is extremely unusual, I've never needed it for real in ten years of Python programming.

Lennart Regebro