day_5.02 py

''' 
2018-5-2 18:43:54
Design 4s shop class
design pattern:
simple factory pattern (separation pattern through a class)

to discuss the problem of coupling between
classes and classes should be low coupling
by having an initialization __init__ to decouple

this is the factory mode
. The parent class method name is the interface, which is implemented
in the subclass (the process is defined in the base class, and then implemented in the subclass)
'''

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("The car is stopping,,,,,,")

class Suonata(Car):
def move(self):
print("The car is moving")
def music(self):
print("The car is playing music")
def stop(self):
print("The car is stopping,,, ,,,")

class Mingtu(Car):
pass
class Ix35(Car):
pass

car_store =CarStore()
car =car_store.order("Sonata")
car.move()
car.music()
car.stop()
bmw_store =BMWCarStore()
bmw =bmw_store.order("720li")

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325217683&siteId=291194637