定義了乙個建立物件的介面,但由子類決定要例項化類的哪乙個;即通過子類來建立物件。
要依賴抽象,不要依賴具體類。
先解釋什麼是工廠:
如果你開一家pizza店(pizzastore抽象類)賣各種風味的pizza(pizza子類),那麼你需要根據客戶要求準備相應的pizza(建立pizza物件),然後烘烤、切片、包裝;
最簡單的做法就是在pizzastore中根據客戶要求(型別判斷)建立相應的pizza物件,然後呼叫pizza自身(由pizza抽象類實現)的烘烤、切片和包裝方法;
但這樣的**缺乏彈性,因為你讓乙個抽象類去依賴具體的物件;我們可以建立乙個工廠來生產pizza,根據傳入的不同型別值返回不同pizza物件,即從pizzastore中將建立物件的**挪到工廠中。但這只是乙個程式設計技巧,並不算模式。
在工廠方法模式中,我們在pizzastore中定義乙個抽象介面(create_pizza)作為抽象的工廠,而order_pizza是它的客戶;將pizza物件的建立放到pizzastore子類去解決。
現有cheese和clam兩款pizza,以及ny和chicago兩家分店,每家店的同款pizza的口味不同——為迎合當地口味做了改進,主要差別來自不同的原材料,因此我們實現四個pizza型別(nystylecheesepizza、nystyleclampizza、chicagostylecheesepizza和chicagostyleclampizza),每種使用不同的原材料組合,根據客戶所在城市和選擇款式我們建立不同的物件;根據工廠方法,我們將物件建立的**放到pizzastore子類去實現。
#!/usr/bin/python
class pizza:
name = ""
dough = ""
sauce = ""
toppings =
def prepare(self):
print "preparing %s" % self.name
print " dough: %s" % self.dough
print " sauce: %s" % self.sauce
print " add toppings:"
for n in self.toppings:
print " %s" % n
def bake(self):
print "bake for 25 minutes at 350."
def cut(self):
print "cutting into diagonal slices."
def box(self):
print "put into official box."
def get_name(self):
return self.name
class pizzastore:
def order_pizza(self, pizza_type):
self.pizza = self.create_pizza(pizza_type)
self.pizza.prepare()
self.pizza.bake()
self.pizza.cut()
self.pizza.box()
return self.pizza
def create_pizza(self, pizza_type):
pass
class nystylecheesepizza(pizza):
def __init__(self):
self.name = "ny style cheese pizza"
self.dough = "ny dough"
self.sauce = "ny sauce"
class chicagostylecheesepizza(pizza):
def __init__(self):
self.name = "chicago style cheese pizza"
self.dough = "chicago dough"
self.sauce = "chicago sauce"
def cut(self):
print "cutting into square slices."
class nystyleclampizza(pizza):
def __init__(self):
self.name = "ny style clam pizza"
self.dough = "ny dough"
self.sauce = "ny sauce"
class chicagostyleclampizza(pizza):
def __init__(self):
self.name = "chicago style clam pizza"
self.dough = "chicago dough"
self.sauce = "chicago sauce"
def cut(self):
print "cutting into square slices."
class nypizzastore(pizzastore):
def create_pizza(self, pizza_type):
if pizza_type == "cheese":
return nystylecheesepizza()
elif pizza_type == "clam":
return nystyleclampizza()
else:
return none
class chicagopizzastore(pizzastore):
def create_pizza(self, pizza_type):
if pizza_type == "cheese":
return chicagostylecheesepizza()
elif pizza_type == "clam":
return chicagostyleclampizza()
else:
return none
if __name__ == "__main__":
ny_store = nypizzastore()
chicago_store = chicagopizzastore()
pizza = ny_store.order_pizza("cheese")
print "mike ordered a %s." % pizza.get_name()
print
pizza = chicago_store.order_pizza("clam")
print "john ordered a %s." % pizza.get_name()
print
preparing ny style cheese pizza
dough: ny dough
sauce: ny sauce
add toppings:
ny toopping a
ny toopping b
bake for 25 minutes at 350.
cutting into diagonal slices.
put into official box.
mike ordered a ny style cheese pizza.
preparing chicago style clam pizza
dough: chicago dough
sauce: chicago sauce
add toppings:
ny toopping a
ny toopping b
chicago toopping a
bake for 25 minutes at 350.
cutting into square slices.
put into official box.
john ordered a chicago style clam pizza.
《head first 設計模式》
設計模式 工廠方法模式
一 工廠方法 factory method 模式 工廠方法模式的意義是定義乙個建立產品物件的工廠介面,將實際建立工作推遲到工廠子類當中。核心工廠類不再負責產品的建立,這樣核心類成為乙個抽象工廠角色,僅負責具體工廠子類必須實現的介面,這樣進一步抽象化的好處是使得工廠方法模式可以使系統在不修改具體工廠角...
設計模式 工廠方法模式
1 factorymethod.h ifndef factorymethod h define factorymethod h include include using namespace std class osproduct 產品,product,產品的抽象類 class windowspro...
設計模式 工廠方法模式
框架的基礎知識 對框架的理解 框架和設計模式的關係 工廠方法模式 定義 定義乙個用於建立物件的介面,讓子類決定例項化哪乙個類,factory method使乙個類的例項化延遲到其子類。結構 產品 public inte ce product 具體產品 public class productimpl...