Singleton / Borg模式基于创建对象时传递的不同参数

时间:2016-06-09 07:19:19

标签: python python-2.7

我正在使用borg模式在对象之间共享状态:

class Borg:
    __shared_state = {}

    def __init__(self):
        self.__dict__ = self.__shared_state

现在让我们假设我想根据创建对象时传递的参数创建Borg类的基于上下文的对象。 这是为多个上下文创建Borg模式(状态共享)的正确方法吗?

import random
import cPickle

class Borg:
    __shared_state = { }

    def __init__(self,*args,**kwargs):
        context_key = hash('{0}{1}'.format(cPickle.dumps(args),cPickle.dumps(kwargs)))
        self.__shared_state.setdefault(context_key, {})
        self.__dict__ = self.__shared_state[context_key]
        print(self.__shared_state)

    def set_random_property(self):
        self.num =  str(random.randint(1,100000))



a = Borg(x='ONE')
a.set_random_property()
b = Borg(x = 'TWO')
b.set_random_property()
c = Borg(x = 'TWO')
print('a with ONE has num:{0}'.format(a.num))
print('b with TWO has num:{0}'.format(b.num))
print('c with TWO has num:{0}'.format(c.num))

输出

{7373348246660160089: {}}
{7373348246660160089: {'num': '18322'}, 3334843421982509183: {}}
{7373348246660160089: {'num': '18322'}, 3334843421982509183: {'num': '33084'}}
a with ONE has num:18322
b with TWO has num:33084
c with TWO has num:33084

正常工作。有没有办法即兴发挥这种模式?或者为python 2.7提供更好的替代方案?

1 个答案:

答案 0 :(得分:2)

不,你用的是我用的东西;使用字典表示共享状态。

您可以使用dict.setdefault()的返回值而不是忽略它来简化稍微

def __init__(self, *args, **kwargs):
    context_key = hash('{0}{1}'.format(cPickle.dumps(args),cPickle.dumps(kwargs)))
    self.__dict__ = self.__shared_state.setdefault(context_key, {})

所有这些都可以封装在元数据类型中:

class PerArgsBorgMeta(type):
    def __new__(mcls, name, bases, attrs):
        cls = super(PerArgsBorgMeta, mcls).__new__(mcls, name, bases, attrs)
        setattr(cls, '_{}__shared_state'.format(name), {})
        return cls

    def __call__(cls, *args, **kwargs):
        instance = super(PerArgsBorgMeta, cls).__call__(*args, **kwargs)
        context_key = hash('{0}{1}'.format(cPickle.dumps(args),cPickle.dumps(kwargs)))
        state = getattr(cls, '_{}__shared_state'.format(cls.__name__))
        instance.__dict__ = state.setdefault(context_key, {})
        return instance

然后将其用作类的__metaclass__属性:

class SomeBorgClass:
    __metaclass__ = PerArgsBorgMeta

请注意,使用hash(cPickle.dumps(kwargs))仍会为带有冲突的词典创建不同的哈希值:

>>> import cPickle
>>> hash(cPickle.dumps({'a': 42, 'i': 81}))
-7392919546006502834
>>> hash(cPickle.dumps({'i': 81, 'a': 42}))
2932616521978949826

同样适用于。排序(递归,如果你必须是详尽的)可以在这里提供帮助,但要小心你不会产生假阳性,例如,作为值传入的集合,以及使用相同值的元组。每种方法都有越来越复杂的解决方法,但在某些时候你只需要接受限制而不是使散列代码更复杂。