Python:borg模式

borg是使用共享属性实现的。

class Borg(object):
    __shared_state = {}

    def __init__(self):
        self.__dict__ = self.__shared_state
        self.state = 'Init'

    def __str__(self):
        return self.state

这种模式下,每个实例的属性字典由类字典代理,每个实例均可操作类字典

你可能感兴趣的:(Python:borg模式)