"""
工厂模式
"""
class NoodleFactory(object):
def __init__(self):
pass
def CreateNoodel(self, sType):
if sType == "拉面":
LaNoodle()
elif sType == "泡面":
PaoNoodle()
elif sType == "炸酱面":
ZhajiangNoodle()
elif sType == "热干面":
ReganNoodle()
class LaNoodle():
def __init__(self):
print("点了一碗拉面")
class PaoNoodle():
def __init__(self):
print("点了一碗泡面")
class ZhajiangNoodle():
def __init__(self):
print("点了一碗炸酱面")
class ReganNoodle():
def __init__(self):
print("点了一碗热干面")
pFactory = NoodleFactory()
lLst = ["泡面", "热干面", "拉面", "炸酱面"]
for sType in lLst:
pFactory.CreateNoodel(sType)
"""
点了一碗泡面
点了一碗热干面
点了一碗拉面
点了一碗炸酱面
"""
headfirsh设计模式——工厂模式例子
最新推荐文章于 2024-07-18 09:44:48 发布