class CarFactory:
__obj = None
__init_flag = True
def creat_car(self,brand):
if brand=="奔驰":
return Benz()#实例化对象
elif brand=="宝马":
return BMW()
elif brand=="比亚迪":
return BYD()
else:
return "未知品牌,无法创建"
def __new__(cls, *args, **kwargs):
if cls.__obj == None:
cls.__obj = object.__new__(cls)
return cls.__obj
def __init__(self):
if CarFactory.__init_flag:
print('init.....')
CarFactory.__init_flag = False
class Benz:
pass
class BMW:
pass
class BYD:
pass
factory=CarFactory()
c1=factory.creat_car('奔驰')
c2=factory.creat_car('比亚迪')
print(c1)
print(c2)
factory2=CarFactory()
print(factory,factory2)
单例模式的一种实现方法