1 ''' 2 2018-5-2 18:43:54 3 设计4s店类 4 设计模式: 5 简单工厂模式(通过一个类的分离模式) 6 7 讨论耦合性的问题 8 类与类之间应该是低耦合性 9 通过有个 初始化 __init__ 来解耦 10 11 这样就是工厂模式 12 父类方法名就是接口,子类里面实现 13 (流程在基类里面定义好,然后在子类里面实现) 14 ''' 15 16 class Store(object): 17 def select_car(self): 18 pass 19 def order(self,car_type): 20 return self.select_car(car_type) 21 22 class BMWCarStore(Store): 23 def select_car(self,car_type): 24 return BMWCarStore().select_car_by_type(car_type) 25 26 27 class CarStore(Store): 28 def select_car(self,car_type): 29 return Factory().select_car_by_type(car_type) 30 31 class BMWFactory(object): 32 def select_car_by_type(self,car_type): 33 pass 34 35 36 class CarStore(object): 37 def __init__(self): 38 self.factory = Factory() 39 def order(self,car_type): 40 return self.factory(car_type) 41 42 class Factory(object): 43 def select_car_by_type(car_type): 44 if car_type=="索纳塔": 45 return Suonata() 46 elif car_type=="名图": 47 return Mingtu() 48 elif car_type=="ix35": 49 return Ix35() 50 51 class Car(object): 52 def move(self): 53 print("车在移动") 54 def music(self): 55 print("车在播放音乐") 56 def stop(self): 57 print("车在停止,,,,,,") 58 59 class Suonata(Car): 60 def move(self): 61 print("车在移动") 62 def music(self): 63 print("车在播放音乐") 64 def stop(self): 65 print("车在停止,,,,,,") 66 67 class Mingtu(Car): 68 pass 69 class Ix35(Car): 70 pass 71 72 car_store =CarStore() 73 car =car_store.order("索纳塔") 74 car.move() 75 car.music() 76 car.stop() 77 bmw_store =BMWCarStore() 78 bmw =bmw_store.order("720li")