现在的位置: 首页 > 综合 > 正文

Python单例模式的4种实现方法

2018年04月15日 ⁄ 综合 ⁄ 共 1651字 ⁄ 字号 评论关闭

Python单例模式的4种实现方法:

方法1: 

实现__new__方法,并将一个类的实例绑定到类变量_instance上。如果cls._instace为None,说明该类还未实例化过,实例化该类,并返回;如果cls._instance不为None,直接返回cls._instance

# -*- coding: utf-8 -*-

class Sington(object):
    def __new__(cls, *args, **kw):
        if not hasattr(cls, '_instance'):
            orig = super(Singleton, cls)
            cls._instance = orig.__new__(cls, args, **kw)
        return cls._instance

class MyClass(Singleton):
    a = 1

_one = MyClass()
_two = MyClass()

_tow.a = 3
print _one.a
print id(_one)
print id(_two)
print _one == _two

方法2(该方法仅是共享属性,并不是实例唯一):

同一个类的所以实例拥有相同的行为,只要保证同一个类的所有实例具有相同的状态即可

所有实例共享属性的最简单直接的方法就是__dict__属性指向(引用)同一个字典(dict)

可参看:http://code.activestate.com/recipes/66531/

Class Borg(object):
    _state = {}
    def __new__(cls, *args, **kw):
        ob = super(Borg, cls).__new__(cls, *args, **kw)
        ob.__dict__ = cls._state
        return ob

class MyClass(Brog):
    a = 1

one = MyClass()
two = MyClass()

two.a = 3
print one.a     #3

print id(one)
print id(two)

print one == two  #False

方法3:

本质上是方法1的升级版

使用__metaclass__(元素)的高级Python用法

class Singleton(type):
    def __init__(cls, name, bases, dict):
        super(Singleton, cls).__init__(name, bases, dict)
        cls._instance = None
    def __call__(cls, *args, **kw):
        if cls._instance is None:
            cls._instance = super(Singleton, cls).__call__(*args, **kw)
        return cls._instance

class MyClass(object):
    __metaclass__ = Singleton2

one = MyClass()
two = MyClass()

two.a = 3
print one.a #3

print id(one)
print id(two)

print one == two #True

方法4:

也是方法1的升级版本

使用装饰器(decorator)

这是一种更pythonic, 更elegant的方法

单例本身根本不知道自己是单例的,因为他本身(自己的代码)并不是单例的

def singleton(cls, *args, **kw):
    instances = {}
    def _singleton():
        if cls not in instances:
            instances[cls] = cls(*args, *kw)
        return instances[cls]
    return _singleton


@singleton
class MyClass(object):
    a = 1
    def __init__(self, x=0):
        self.x = x

one = MyClass()
two = MyClass()

two.a = 3
print one.a

print id(one)
print id(two)

print one == two
print one is two

one.x = 1

print two.x

【上篇】
【下篇】

抱歉!评论已关闭.