headfirsh设计模式——抽象工厂模式例子

"""
抽象工厂模式
"""
class PizzaIngredientFactory(object):
	def __init__(self):
		pass

	def createDough(self):
		pass

	def createSauce(self):
		pass

	def createCheese(self):
		pass

	def createVeggies(self):
		pass
	
	def createPepperoni(self):
		pass

	def createClam(self):
		pass

class NYPizzaIngredientFactory(PizzaIngredientFactory):
	def __init__(self):
		pass

	def createDough(self):
		print("制作Pizza生面团")

	def createSauce(self):
		print("制作番茄酱")		

	def createCheese(self):
		print("制作芝士")		

	def createVeggies(self):
		print("准备蒜头,洋葱,蘑菇,红辣椒")
	
	def createPepperoni(self):
		print("制作香肠")

	def createClam(self):
		print("制作新鲜蛤蜊")		

class Pizza(object):
	def __init__(self):
		self.name = ""
		self.sauce = None
		self.veggies = []
		self.cheese = None
		self.popperoni = None
		self.clam = None

	def prepare(self):
		pass

	def bake(self):
		print("烘烤披萨")

	def cut(self):
		print("把披萨切开")

	def box(self):
		print("把披萨放盒里")

	def setName(self, sName):
		self.name = sName

	def getName(self):
		return self.name

	def toString(self):
		pass

class CheesePizza(Pizza):
	def __init__(self, IngredientFactory):
		self.Factory = IngredientFactory
		self.prepare()

	def prepare(self):
		dough = self.Factory.createDough()
		sauce = self.Factory.createSauce()
		cheese = self.Factory.createCheese()

class ClamPizza(Pizza):
	def __init__(self, IngredientFactory):
		self.Factory = IngredientFactory
		self.prepare()

	def prepare(self):
		dough = self.Factory.createDough()
		sauce = self.Factory.createSauce()
		cheese = self.Factory.createCheese()
		clam = self.Factory.createClam() 

class PizzaStore(object):
	def __init__(self):
		pass

	def orderPizza(self, type):
		pizza = createPizza(type)
		pizza.prepare()
		pizza.bake()
		pizza.cut()
		pizza.box()
	
	def createPizza(self, type):
		return Pizza()

class NYPizzaStore(PizzaStore):
	def __init__(self):
		pass

	def createPizza(self, type):
		pizza = None
		ingredientFactory = NYPizzaIngredientFactory()
		if type == "cheese":
			pizza = CheesePizza(ingredientFactory)
			pizza.setName("芝士Pizza")
		elif type == "clam":
			pizza = ClamPizza(ingredientFactory)
			pizza.setName("蛤蜊Pizza")

		return pizza

pStore = NYPizzaStore()
pizza1 = pStore.createPizza("cheese")
print('Pizza是' + pizza1.getName())
print('------------------------------------')
pizza2 = pStore.createPizza("clam")
print('Pizza2是' + pizza2.getName())

"""
制作Pizza生面团
制作番茄酱
制作芝士
Pizza是芝士Pizza
------------------------------------
制作Pizza生面团
制作番茄酱
制作芝士
制作新鲜蛤蜊
Pizza2是蛤蜊Pizza
"""
上一篇:[Windows]Allow Administrator account mstsc multi login / 允许Administrator账号多用户远程登录


下一篇:用script标签加载