day_5.02 py

时间:2022-06-24 09:36:59
 '''
2018-5-2 18:43:54
设计4s店类
设计模式:
简单工厂模式(通过一个类的分离模式) 讨论耦合性的问题
类与类之间应该是低耦合性
通过有个 初始化 __init__ 来解耦 这样就是工厂模式
父类方法名就是接口,子类里面实现
(流程在基类里面定义好,然后在子类里面实现)
''' 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 BMWCarStore().select_car_by_type(car_type) class CarStore(Store):
def select_car(self,car_type):
return Factory().select_car_by_type(car_type) class BMWFactory(object):
def select_car_by_type(self,car_type):
pass class CarStore(object):
def __init__(self):
self.factory = Factory()
def order(self,car_type):
return self.factory(car_type) class Factory(object):
def select_car_by_type(car_type):
if car_type=="索纳塔":
return Suonata()
elif car_type=="名图":
return Mingtu()
elif car_type=="ix35":
return Ix35() class Car(object):
def move(self):
print("车在移动")
def music(self):
print("车在播放音乐")
def stop(self):
print("车在停止,,,,,,") class Suonata(Car):
def move(self):
print("车在移动")
def music(self):
print("车在播放音乐")
def stop(self):
print("车在停止,,,,,,") class Mingtu(Car):
pass
class Ix35(Car):
pass car_store =CarStore()
car =car_store.order("索纳塔")
car.move()
car.music()
car.stop()
bmw_store =BMWCarStore()
bmw =bmw_store.order("720li")