Python 设计模式 --抽象工厂设计模式

   抽象工厂同简单工厂模式的最大区别是 : 简单工厂模式 或者 工厂方法是 通过父类,屏蔽子类,创建单个产品。

   抽象工厂是创建一个产品族。

   UML 类图如下:

 

抽象产品 AbstractPrd 的定义,我们将其定义为Pissza

AbstractPrd
 1  class Pizza:  
 2 
 3     name = ""  
 4     dough = None  
 5     sauce = None  
 6     cheese = None  
 7     clam = None  
 8   
 9     def prepare(self):  
10         pass  
11   
12     def bake(self):  
13         print "Bake for 25 minutes at 350."  
14   
15     def cut(self):  
16         print "Cutting into diagonal slices."  
17   
18     def box(self):  
19         print "Put into official box."  
20   
21     def get_name(self):  
22         return self.name  
23   
24     def set_name(self, name):  
25         self.name = name  
26   
27     def to_string(self):  
28         string = "%s:\n" % self.name   
29         string += "    Dough: %s\n" % self.dough.to_string() if self.dough else ""  
30         string += "    Sauce: %s\n" % self.sauce.to_string() if self.sauce else ""  
31         string += "    Cheese: %s\n" % self.cheese.to_string() if self.cheese else ""  
32         string += "    Clam: %s\n" % self.clam.to_string() if self.clam else ""  
33         return string 

 

定义Pissza 的两个子类 , CheesePissa ,PalmPissa

Prod
 1 class CheesePizza(Pizza):  
 2    def __init__(self, ingredient_factory):  
 3         self.ingredient_factory = ingredient_factory  
 4   
 5     def prepare(self):  
 6         print "Preparing: %s" % self.name  
 7         self.dough = self.ingredient_factory.create_dough()  
 8         self.sauce = self.ingredient_factory.create_sauce()  
 9         self.cheese = self.ingredient_factory.create_cheese()  
10   
11   
12 class ClamPizza(Pizza):  
13     def __init__(self, ingredient_factory):  
14         self.ingredient_factory = ingredient_factory  
15   
16     def prepare(self):  
17         print "Preparing: %s" % self.name  
18         self.dough = self.ingredient_factory.create_dough()  
19         self.sauce = self.ingredient_factory.create_sauce()  
20         self.clam = self.ingredient_factory.create_clam() 

 

创建Pissza的抽象工厂父类

AbstractFactory
 1 class PizzaStore:  
 2     def order_pizza(self, pizza_type):  
 3         self.pizza = self.create_pizza(pizza_type)  
 4         self.pizza.prepare()  
 5         self.pizza.bake()  
 6         self.pizza.cut()  
 7         self.pizza.box()  
 8         return self.pizza  
 9   
10     def create_pizza(self, pizza_type):  
11         pass  


创建一个产品族的具体工厂

View Code
class NYPizzaStore(PizzaStore):  

    def create_pizza(self, pizza_type):  
        ingredient_factory = NYPizzaIngredientFactory()  
  
        if pizza_type == "cheese":  
            pizza = CheesePizza(ingredient_factory)  
            pizza.set_name("New York Style Cheese Pizza")  
        elif pizza_type == "clam":  
            pizza = ClamPizza(ingredient_factory)  
            pizza.set_name("New York Style Clam Pizza")  
        else:  
            pizza = None  
  
        return pizza  
  
  
class ChicagoPizzaStore(PizzaStore):  
     def create_pizza(self, pizza_type):  
        ingredient_factory = ChicagoPizzaIngredientFactory()  
  
        if pizza_type == "cheese":  
            pizza = CheesePizza(ingredient_factory)  
            pizza.set_name("Chicago Style Cheese Pizza")  
        elif pizza_type == "clam":  
            pizza = ClamPizza(ingredient_factory)  
            pizza.set_name("Chicago Style Clam Pizza")  
        else:  
            pizza = None  
  
        return pizza  

 

 

 

简单也就这样,还可以更复杂!不过还是简单就是美吧!

 

  

 

 

 

 

posted @ 2013-01-23 16:39  jerry_xing8  阅读(1181)  评论(0编辑  收藏  举报