#!/usr/bin/env python
# -- coding = 'utf-8' --
# Author Allen Lee
# Python Version 3.5.1
# OS Windows 7
#设计模式,单例模式,单实例
class Foo:
instance = None
def __init__(self,name):
self.name = name
@classmethod
def get_instance(cls):
if cls.instance:
return cls.instance
else:
obj = cls('alex')
cls.instance = obj
return obj
obj1 = Foo.get_instance()
print(obj1)
obj2 = Foo.get_instance()
print(obj2)
Python案例-开发之路-设计模式-单例模式
最新推荐文章于 2023-04-30 18:45:25 发布