设计模式(六):单件模式-创新互联

 有时候你需要系统只产生一个类的实例,比如你的手机上面所有的应用,只能共享同一份电话薄。设计模式(六):单件模式

 python的代码很简单:

成都创新互联公司长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为渝北企业提供专业的网站建设、网站制作,渝北网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。

http://blog.csdn.net/insistgogo/article/details/9412863
def Singleton( cls ): instance= {} def GetInstance(): if cls not in instance: instance[cls]= cls() return instance[cls] return GetInstance @Singleton class SingletonCls(object): """docstring for SingletonCls""" def __init__(self ): self.name= 'This is singleton.' def GetName( self ): return self.name if __name__ == '__main__': phoneNumber= SingletonCls() print phoneNumber.GetName() phoneNumber1= SingletonCls() if phoneNumber1 == phoneNumber: print "The same instance." else: print "Not the same."

名称栏目:设计模式(六):单件模式-创新互联
当前地址:http://myzitong.com/article/dccghi.html