Python 设计模式 单例

1.单例模式 

class single:
    __instance = None
    def __init__(self):
        pass

    @staticmethod
    def get_instance():
        if single.__instance :
            return single.__instance
        else:
            single.__instance = single()
            return single.__instance

obj1 = single.get_instance()
print(obj1) # <__main__.sigle object at 0x0000000003456EB8>
obj2 = single.get_instance()
print(obj1) # <__main__.sigle object at 0x0000000003456EB8>

 通过静态字段和静态方法创建,在内存中永远只创建一份实例

posted @ 2017-05-02 11:05  1916  阅读(80)  评论(0)    收藏  举报