python单例模式

转载:https://www.cnblogs.com/pinking/p/7820498.html

一、单例模式

单例模式是应用开发过程中最简单同时也是最著名的一种创建型设计模式。单例模式提供了一个类只有一个特定类型的对象的机制。

通常应用于场景为:日志记录、数据库操作等,程序运行时只能生成一个实例,避免对同一资源产生冲突的访问请求。

二、如何设计单例模式

(1)重写构造函数__new__方法

1
2
3
4
5
6
7
8
9
10
11
12
class Singleton( object ):
     def __new__( self ):
         if not hasattr ( self , 'instance' ):
             self .instance = super (Singleton, self ).__new__( self )
         return self .instance
         
 
a = Singleton()
b = Singleton()
 
 
print id (a), id (a)

  该方法主要思路就是在覆盖实例化函数__new__(),在其中添加判断,检查对象是否存在。hasattr是python的特殊方法,用来查看对象是否具有某个属性。

当然,如果想要继承这个Singleton,子类中一定不要忘了重写__new__方法,否则会覆盖掉父类中已经修改的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Singleton( object ):
     def __new__( self ):
         if not hasattr ( self , 'instance' ):
             self .instance = super (Singleton, self ).__new__( self )
         return self .instance
         
class test(Singleton):
     def __new__( self ):
         super (Singleton, self ).__new__( self )
a = test()
b = test()
 
 
print id (a), id (a)

  

(2)元类编程重写__call__方法

元类是一个类的类,可以通过创建元类,重新定义类的行为。当我们用类A创建一个类的时候,python通过A = type( name , bases , dict )创建它,其中name是类的名称,base是基类,dict是属性变量。

下面讨论元类type构造类以及实例的过程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Meta( type ):
     def __init__( self , name, bases, attrs):
         print "init"
         super (Meta, self ).__init__(name, bases, attrs)
     def __new__(meta,name, bases, attrs):
         print "new"
         return super (Meta,meta).__new__(meta,name, bases, attrs)
     def __call__( self ):
         print "call"
         return super (Meta, self ).__call__()
         
         
s = [ "Student" , ( object , ), { "name" : "Joe" , "age" : 25 }   ]   
ClassStudent = Meta( * s)
instance = ClassStudent()

  可以看到,结果是:

这个过程是,首先__new__创建类的实例,作为__init__的输入,完成构造函数的作用。当然这个过程的结果是获得了一个实例并且是叫做:

Student的类,ClassStudent是对这个类的引用。当采用()调用,即实现Meta的实例的可调用的实例化过程。

至此,我们简单了解了相关的魔术函数的作用。

 

通过元类实现单例模式的方法为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Meta( type ):
     _instance = {}
     def __init__( self , name, bases, attrs):
         print "init"
         super (Meta, self ).__init__(name, bases, attrs)
     def __new__(meta,name, bases, attrs):
         print "new"
         return super (Meta,meta).__new__(meta,name, bases, attrs)
     def __call__( self ):
         print "call"
         
         if self not in self ._instance:
             self ._instance[ self ] = super (Meta, self ).__call__()
         return self ._instance[ self ]
         
         
s = [ "Student" , ( object , ), { "name" : "Joe" , "age" : 25 }   ]   
ClassStudent = Meta( * s)
instance1 = ClassStudent()
instance2 = ClassStudent()
print id (instance1), id (instance2)

  至此,可以看到,通过元类编程实现单例模式,需要改的是__call__函数,因为其构造函数是为了构造类的,而不是在生成实例的过程中,通过函数调用的方式生成实例会调用__call__,因此在这个位置操作。

 当然,对于单例类的使用,可以在类中通过__metaclass__属性来设置,取代用type类直接生成的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Meta( type ):
     _instance = {}
     def __init__( self , name, bases, attrs):
         print "init"
         super (Meta, self ).__init__(name, bases, attrs)
     def __new__(meta,name, bases, attrs):
         print "new"
         return super (Meta,meta).__new__(meta,name, bases, attrs)
     def __call__( self ):
         print "call"
         
         if self not in self ._instance:
             self ._instance[ self ] = super (Meta, self ).__call__()
         return self ._instance[ self ]
  
 
class ClassStudent( object ):
     __metaclass__ = Meta
     pass
        
instance1 = ClassStudent()
instance2 = ClassStudent()
print id (instance1), id (instance2)

  当用户定义一个类class的时候,Python解释器就会在当前类中查找"__metaclass__"属性,如果找到,就通过该属性对应的代码创建类;如果没有找到,就继续以相同的规则查找父类。如果在任何父类中都找不到"__metaclass__",就会用内置的type来创建类对象。

当然对于python3来说,添加了新的声明单例的方法:

1
2
class Test(metaclass = MyMeta):
     pass

  

三、Monostate模式

对于上述单例模式,关注的是生成单一的实例,而通常程序员需要的是让实例共享相同的状态,因此,有时候需要关注状态和行为,而不是同一性。这种概念即为Monostate(单态)模式。python实现这个模式的过程较为轻松:

1
2
3
4
5
6
7
8
class ClassStudent( object ):
     _age = 20
     pass
        
instance1 = ClassStudent()
instance2 = ClassStudent()
print id (instance1), id (instance2)
print instance1._age,instance2._age

  可以看到,虽然不是一个实例,但是状态相同。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值