Python Singleton

 

python学习教程(十三)python实现单例模式 

  5077人阅读  评论(2)  收藏  举报
  分类:
 

首先,要知道什么是python的单例模式,所谓单例模式就是一个类只能创建一个实例化。

然后,就是python单例模式的方法,总共可以分为两大种,四小种,一会就回说的。

首先,方法一:

[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. class Singleton(type):  
  2.     def __init__(cls, name, bases, dict):  
  3.     super(Singleton, cls).__init__(name, bases, dict)  
  4.     cls.instance = None  
  5.   
  6.     def __call__(cls, *args, **kw):  
  7.     if cls.instance is None:  
  8.         cls.instance = super(Singleton, cls).__call__(*args, **kw)  
  9.         return cls.instance  
  10.   
  11. class MyClass(object):  
  12.     __metaclass__ = Singleton  
  13.   
  14. print MyClass()  
  15. print MyClass()  



方法二:使用装饰器( decorator

[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. def singleton(cls, *args, **kw):    
  2.     instances = {}    
  3.     def _singleton():    
  4.         if cls not in instances:    
  5.             instances[cls] = cls(*args, **kw)    
  6.         return instances[cls]    
  7.     return _singleton    
  8.   
  9. @singleton    
  10. class MyClass(object):    
  11.     a = 1    
  12.     def __init__(self, x=0):    
  13.         self.x = x    
  14.     
  15. one = MyClass()    
  16. two = MyClass()    
  17.     
  18. two.a = 3    
  19. print one.a    
  20. #3    
  21. print id(one)    
  22. #29660784    
  23. print id(two)    
  24. #29660784    
  25. print one == two    
  26. #True    
  27. print one is two    
  28. #True    
  29. one.x = 1    
  30. print one.x    
  31. #1    
  32. print two.x  

方法三:使用__metaclass__元类来实现

[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. class Singleton2(type):    
  2.     def __init__(cls, name, bases, dict):    
  3.         super(Singleton2, cls).__init__(name, bases, dict)    
  4.         cls._instance = None    
  5.     def __call__(cls, *args, **kw):    
  6.         if cls._instance is None:    
  7.             cls._instance = super(Singleton2, cls).__call__(*args, **kw)    
  8.         return cls._instance    
  9.     
  10. class MyClass(object):    
  11.     __metaclass__ = Singleton2    
  12.     
  13. one = MyClass()    
  14. two = MyClass()    
  15.     
  16. two.a = 3    
  17. print one.a    
  18. #3    
  19. print id(one)    
  20. #31495472    
  21. print id(two)    
  22. #31495472    
  23. print one == two    
  24. #True    
  25. print one is two    
  26. #True    

方法四:通过共享属性来实现,所谓共享属性,最简单直观的方法就是通过__dict__属性指向同一个字典dict

[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. class Borg(object):    
  2.     _state = {}    
  3.     def __new__(cls, *args, **kw):    
  4.         ob = super(Borg, cls).__new__(cls, *args, **kw)    
  5.         ob.__dict__ = cls._state    
  6.         return ob    
  7.     
  8. class MyClass(Borg):    
  9.     a = 1    
  10.     
  11. one = MyClass()    
  12. two = MyClass()    
  13.     
  14. #one和two是两个不同的对象,id, ==, is对比结果可看出    
  15. two.a = 3    
  16. print one.a    
  17. #3    
  18. print id(one)    
  19. #28873680    
  20. print id(two)    
  21. #28873712    
  22. print one == two    
  23. #False    
  24. print one is two    
  25. #False    
  26. #但是one和two具有相同的(同一个__dict__属性),见:    
  27. print id(one.__dict__)    
  28. #30104000    
  29. print id(two.__dict__)    

其实吧,从本质上来讲,方法一二三都属于一种单例化模式的方法,与第四种不同,所以认为python中有两种或四种方法实现单例模式都可以。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值