为什么在Python中Borg模式比Singleton模式更好

10 浏览
0 Comments

为什么在Python中Borg模式比Singleton模式更好

为什么Borg模式Singleton模式更好?

我问这个问题是因为我没有看到它们的结果有任何不同。

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

我想在这里展示的是,无论是作为Borg还是Singleton实现的服务对象,都具有非平凡的内部状态(基于它提供某些服务)(我的意思是它必须是有用的,而不是Singleton/Borg只是为了好玩)。

而且,这个状态必须是初始化的。在这里,Singleton实现更加直接,因为我们将init视为全局状态的设置。我觉得Borg对象必须查询其内部状态来看是否需要更新自己,这样做让人感到尴尬。

如果有更多的内部状态,情况会变得更糟糕。例如,如果对象必须监听应用程序的撤销信号,以将其注册保存到磁盘上,那么该注册也只应该完成一次。使用Singleton会更容易。

admin 更改状态以发布 2023年5月22日
0
0 Comments

在Python中,如果你想要一个可以从任何地方访问的唯一的“对象”,那么只需创建一个只包含静态属性、@staticmethods和@classmethods的类Unique,你可以称之为唯一模式。在这里,我实现并比较了三种模式:

Unique

#Unique Pattern
class Unique:
#Define some static variables here
    x = 1
    @classmethod
    def init(cls):
        #Define any computation performed when assigning to a "new" object
        return cls

Singleton

#Singleton Pattern
class Singleton:
    __single = None 
    def __init__(self):
        if not Singleton.__single:
            #Your definitions here
            self.x = 1 
        else:
            raise RuntimeError('A Singleton already exists') 
    @classmethod
    def getInstance(cls):
        if not cls.__single:
            cls.__single = Singleton()
        return cls.__single

Borg

#Borg Pattern
class Borg:
    __monostate = None
    def __init__(self):
        if not Borg.__monostate:
            Borg.__monostate = self.__dict__
            #Your definitions here
            self.x = 1
        else:
            self.__dict__ = Borg.__monostate

Test

#SINGLETON
print "\nSINGLETON\n"
A = Singleton.getInstance()
B = Singleton.getInstance()
print "At first B.x = {} and A.x = {}".format(B.x,A.x)
A.x = 2
print "After A.x = 2"
print "Now both B.x = {} and A.x = {}\n".format(B.x,A.x)
print  "Are A and B the same object? Answer: {}".format(id(A)==id(B))
#BORG
print "\nBORG\n"
A = Borg()
B = Borg()
print "At first B.x = {} and A.x = {}".format(B.x,A.x)
A.x = 2
print "After A.x = 2"
print "Now both B.x = {} and A.x = {}\n".format(B.x,A.x)
print  "Are A and B the same object? Answer: {}".format(id(A)==id(B))
#UNIQUE
print "\nUNIQUE\n"
A = Unique.init()
B = Unique.init()
print "At first B.x = {} and A.x = {}".format(B.x,A.x)
A.x = 2
print "After A.x = 2"
print "Now both B.x = {} and A.x = {}\n".format(B.x,A.x)
print  "Are A and B the same object? Answer: {}".format(id(A)==id(B))

输出:

SINGLETON

At first B.x = 1 and A.x = 1
After A.x = 2
Now both B.x = 2 and A.x = 2
Are A and B the same object? Answer: True
BORG
At first B.x = 1 and A.x = 1
After A.x = 2
Now both B.x = 2 and A.x = 2
Are A and B the same object? Answer: False
UNIQUE
At first B.x = 1 and A.x = 1
After A.x = 2
Now both B.x = 2 and A.x = 2
Are A and B the same object? Answer: True

在我看来,Unique的实现最简单,然后是Borg,最后是Singleton,需要定义它的两个函数数量比较丑陋。

0
0 Comments

博格模式之所以与其他模式不同,关键在于子类化。

如果你对博格模式进行子类化,子类对象的状态与其父类对象相同,除非你在子类中显式地覆盖共享状态。每个单例模式的子类都有自己的状态,因此将生成不同的对象。

另外,在单例模式中,对象实际上是相同的,不仅状态相同(尽管状态是唯一真正重要的事情)。

0