美文网首页
python基础-工厂模式

python基础-工厂模式

作者: ___Kevin | 来源:发表于2018-02-27 14:10 被阅读0次

class Car(object):
'''
def init(self,color,speed):
self.color=color
self.speed=speed
'''

def move(self):
    print("car moving ...")

def whistle(self):
    print("car whistling ...") 

class Store(object):
def select_car(self):
pass

def order(self,car_type):
    return self.select_car(car_type)

class BMWCarStore(Store):
def select_car(self,car_type):
return BMWFactory().select_car_by_type(car_type)

class BMWFactory(object):
def select_car_by_type(self,car_type):
if car_type=="mini":
return Suonata()
elif car_type=="720li":
return Mingtu()
else:
return Ix35()

class Suonata(Car):
pass

class Mingtu(Car):
pass

class Ix35(Car):
pass

==================================================

class OtherCarStore(Store):
def select_car(self,car_type):
return OtherFactory().select_car_by_type(car_type)

class OtherFactory(object):
def select_car_by_type(self,car_type):
if car_type=="1":
return Car1()
elif car_type=="2":
return Car2()
else:
return Car0()

class Car1(Car):
pass

class Car2(Car):
pass

class Car0(Car):
pass

==================================================

bmw_store=BMWCarStore()
bmw=bmw_store.order("720li")

other_store=OtherCarStore()
other=other_store.order("2")

print(bmw)
print(other)

bmw.move()
other.move()

相关文章

网友评论

      本文标题:python基础-工厂模式

      本文链接:https://www.haomeiwen.com/subject/kozpxftx.html