python基础-工厂模式

2018-02-27  本文已影响0人  ___Kevin

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()

上一篇 下一篇

猜你喜欢

热点阅读