python实现单例工厂模式
class CarFactory: '''python实现单例工厂模式''' __obj = None __flg_init = True def __new__(cls, *args, **kwargs): if cls.__obj is None: cls.__obj = object.__new__(CarFactory) return cls.__obj def __init__(self): if CarFactory.__flg_init: print('工厂产生了') CarFactory.__flg_init = False def creatCar(self, car_name): if car_name == 'Benz': car = Benz() elif car_name == 'BMW': car = BMW() else: car = BYD() return car class Benz: def __init__(self): print('奔驰产生了') class BMW: def __init__(self): print('宝马产生了') class BYD: def __init__(self): print('比亚迪产生了') cf = CarFactory() cf2 = CarFactory() print(cf) print(cf2) cf.creatCar('123') cf2.creatCar('Benz')
相关推荐
缘起宇轩阁 2019-12-30
yangyujun 2020-08-21
jkzyx 2020-04-17
swimmer000 2019-12-30
王志龙 2019-12-26
gougouzhang 2019-12-13
徐建岗网络管理 2011-12-18
HuangXiaoChuan 2019-03-20
一个过客 2019-03-25
machao 2019-03-21
cyAspNet 2019-07-01
Triones 2019-07-01
HavenZhao 2019-07-01
chengrile 2014-10-26
programisaart 2019-06-28
fuziwang 2019-06-28
gotea 2012-09-13