传智播客旗下技术交流社区北京校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 梦缠绕的时候 黑马程序员官方团队   /  2019-1-9 10:04  /  104 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


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车在停止....

 

"""
分享至 : QQ空间
收藏

1 个回复

倒序浏览
提示: 作者被禁止或删除 内容自动屏蔽
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马