Entity
class Car(object):
def move(self):
pass
def music(self):
pass
def stop(self):
pass
from com.day11.factoryMethod.entity.Car import Car
class Ix35(Car):
def move(self):
print("Ix35车在移动....")
def music(self):
print("Ix35正在播放音乐....")
def stop(self):
print("Ix35车在停止....")
from com.day11.factoryMethod.entity.Car import Car
class Mingtu(Car):
def move(self):
print("Mingtu车在移动....")
def music(self):
print("Mingtu正在播放音乐....")
def stop(self):
print("Mingtu车在停止....")
from com.day11.factoryMethod.entity.Car import Car
class Mini(Car):
def move(self):
print("Mini车在移动....")
def music(self):
print("Mini正在播放音乐....")
def stop(self):
print("Mini车在停止....")
from com.day11.factoryMethod.entity.Car import Car
class Suonata(Car):
def move(self):
print("Suonata车在移动....")
def music(self):
print("Suonata正在播放音乐....")
def stop(self):
print("Suonata车在停止....")
from com.day11.factoryMethod.entity.Car import Car
class SuvX6(Car):
def move(self):
print("SUVX6车在移动....")
def music(self):
print("SUVX6正在播放音乐....")
def stop(self):
print("SUVX6车在停止....")
Factory
from com.day11.factoryMethod.entity.Mini import Mini
from com.day11.factoryMethod.entity.SuvX6 import SuvX6
class BMWCarFactory(object):
def select_car_by_type(self, car_type):
if car_type == "mini":
return Mini()
elif car_type == "x6":
return SuvX6()
from com.day11.factoryMethod.entity.Mingtu import Mingtu
from com.day11.factoryMethod.entity.Ix35 import Ix35
from com.day11.factoryMethod.entity.Suonata import Suonata
class XianDaiFactory(object):
def select_car_by_type(self, car_type):
if car_type == "索纳塔":
return Suonata()
elif car_type == "名图":
return Mingtu()
elif car_type == "ix35":
return Ix35()
Store
class Store(object):
def select_car(self):
pass
def order(self, car_type):
return self.select_car(car_type)
from com.day11.factoryMethod.factory.BMWCarFactory import BMWCarFactory
from com.day11.factoryMethod.store.Store import Store
class BMWCarStore(Store):
def select_car(self, car_type):
return BMWCarFactory().select_car_by_type(car_type)
from com.day11.factoryMethod.factory.XianDaiFactory import XianDaiFactory
from com.day11.factoryMethod.store.Store import Store
class XianDaiCarStore(Store):
def select_car(self, car_type):
return XianDaiFactory().select_car_by_type(car_type)
测试
# 测试宝马
from com.day11.factoryMethod.store.BMWCarStore import BMWCarStore
from com.day11.factoryMethod.store.XianDaiCarStore import XianDaiCarStore
store1 = BMWCarStore()
mini = store1.order("mini")
mini.move()
mini.music()
mini.stop()
x6 = store1.order("x6")
x6.move()
x6.music()
x6.stop()
# 测试现代
store1 = XianDaiCarStore()
mingtu = store1.order("名图")
mingtu.move()
mingtu.music()
mingtu.stop()
"""
Mini车在移动....
Mini正在播放音乐....
Mini车在停止....
SUVX6车在移动....
SUVX6正在播放音乐....
SUVX6车在停止....
Mingtu车在移动....
Mingtu正在播放音乐....
Mingtu车在停止....
"""
抽象工厂模式
最新推荐文章于 2024-08-17 15:45:47 发布