舉例講解Python設計模式編程中對抽象原廠模式的運用

來源:互聯網
上載者:User
抽象原廠模式:提供一個建立一系列相關或相互依賴對象的介面,而無需指定它們具體的類。
優點:易於交換“產品系列”,只要更改相應的工廠即可。
缺點:建立產品的時候很繁瑣,需要增加和修改很多東西。

最佳化1:為了避免用戶端有過多的邏輯判斷,可以封裝出一個簡單工廠類來產生產品類。
最佳化2:為了減少簡單工廠類裡面的邏輯判斷,可以採用“反射”機制,直接根據外部的設定檔讀取出需要使用產品類的資訊。

#encoding=utf-8 # #by panda #抽象原廠模式  def printInfo(info):   print unicode(info, 'utf-8').encode('gbk')    #抽象產品A:user表 class IUser():   def Insert(self):     pass   def GetUser(self):     pass  #sqlserver實現的User class SqlserverUser(IUser):   def Insert(self):     printInfo("在SQL Server中給User表增加一條記錄")   def GetUser(self):     printInfo("在SQL Server中得到User表的一條記錄")  #Access實現的User class AccessUser(IUser):   def Insert(self):     printInfo("在Access中給User表增加一條記錄")   def GetUser(self):     printInfo("在Access中得到User表一條記錄")   #抽象產品B:部門表 class IDepartment():   def Insert(self):     pass   def GetUser(self):     pass  #sqlserver實現的Department class SqlserverDepartment(IUser):   def Insert(self):     printInfo("在SQL Server中給Department表增加一條記錄")   def GetUser(self):     printInfo("在SQL Server中得到Department表的一條記錄")  #Access實現的Department class AccessDepartment(IUser):   def Insert(self):     printInfo("在Access中給Department表增加一條記錄")   def GetUser(self):     printInfo("在Access中得到Department表一條記錄")   #抽象工廠 class IFactory():   def CreateUser(self):     pass   def CreateDepartment(self):     pass    #sql server工廠 class SqlServerFactory(IFactory):   def CreateUser(self):     return SqlserverUser()   def CreateDepartment(self):     return SqlserverDepartment()  #access工廠 class AccessFactory(IFactory):   def CreateUser(self):     return AccessUser()   def CreateDepartment(self):     return AccessDepartment()    #最佳化一:採用一個簡單工廠類,封裝邏輯判斷操作 class DataAccess(): #  db = "Sqlserver"   db = "Access"   @staticmethod   def CreateUser():     if (DataAccess.db == "Sqlserver"):       return SqlserverUser()     elif(DataAccess.db == "Access"):       return AccessUser()   @staticmethod   def CreateDepartment():     if (DataAccess.db == "Sqlserver"):       return SqlserverDepartment()     elif(DataAccess.db == "Access"):       return AccessDepartment()      #最佳化二:採用反射機制,避免使用太多判斷 ##以下資訊可以從設定檔中擷取 DBType = 'Sqlserver' #'Access' DBTab_User = 'User' DBTab_Department = 'Department'  class DataAccessPro(): #  db = "Sqlserver"   db = "Access"   @staticmethod   def CreateUser():     funName = DBType + DBTab_User     return eval(funName)() #eval 將其中的字串轉化為python運算式   @staticmethod   def CreateDepartment():     funName = DBType + DBTab_Department     return eval(funName)()      def clientUI():   printInfo("\n--------抽象Factory 方法--------")   factory = SqlServerFactory()   iu = factory.CreateUser()   iu.Insert()   iu.GetUser()   id = factory.CreateDepartment()   id.Insert()   id.GetUser()      printInfo("\n--抽象Factory 方法+簡單Factory 方法--")   iu = DataAccess.CreateUser()   iu.Insert()   iu.GetUser()   id = DataAccess.CreateDepartment()   id.Insert()   id.GetUser()      printInfo("\n-抽象Factory 方法+簡單Factory 方法+反射-")   iu = DataAccessPro.CreateUser()   iu.Insert()   iu.GetUser()   id = DataAccessPro.CreateDepartment()   id.Insert()   id.GetUser()     return   if __name__ == '__main__':   clientUI(); 

類圖:

原廠模式和抽象原廠模式的區別:原廠模式是在衍生類別中定義一個工廠的抽象介面,然後基類負責建立具體對象;抽象原廠模式是維護一個產品家族,由基類定義產品被生產的方法,客戶根據衍生類別的介面進行開發。

執行個體:人民群眾喜聞樂見的披薩店例子這裡又可以搬出來了,這次我們根據抽象原廠模式的特點,用不同原材料製作不同口味的披薩,建立不同原材料的工廠,不同實體店做出口味不同的披薩。建立一個產品家族(Dough、Sauce、Cheese和Clam)的抽象類別型(PizzaIngredientFactory),這個類型的子類(NYPizzaIngredientFactory和ChicagoPizzaIngredientFactory)定義了產品被產生的方法。
代碼:

#!/usr/bin/python# -*- coding:utf-8 -*-import sysreload(sys)sys.setdefaultencoding('utf-8') '''披薩'''class Pizza:  name = ""  dough = None  sauce = None  cheese = None  clam = None   def prepare(self):    pass   def bake(self):    print "烘烤25分鐘在350。".decode('utf-8')   def cut(self):    print "切割成對角線切片。".decode('utf-8')   def box(self):    print "放在官方的盒子中。".decode('utf-8')   def get_name(self):    return self.name   def set_name(self, name):    self.name = name   def to_string(self):    string = "%s:\n" % self.name    string += "  麵糰: %s\n" % self.dough.to_string() if self.dough else ""    string += "  醬汁: %s\n" % self.sauce.to_string() if self.sauce else ""    string += "  乳酪: %s\n" % self.cheese.to_string() if self.cheese else ""    string += "  文蛤: %s\n" % self.clam.to_string() if self.clam else ""    return string '''什麼類別的披薩'''class CheesePizza(Pizza):  def __init__(self, ingredient_factory):    self.ingredient_factory = ingredient_factory   def prepare(self):    print "準備: %s" % self.name    self.dough = self.ingredient_factory.create_dough()    self.sauce = self.ingredient_factory.create_sauce()    self.cheese = self.ingredient_factory.create_cheese()  class ClamPizza(Pizza):  def __init__(self, ingredient_factory):    self.ingredient_factory = ingredient_factory   def prepare(self):    print "準備: %s" % self.name    self.dough = self.ingredient_factory.create_dough()    self.sauce = self.ingredient_factory.create_sauce()    self.clam = self.ingredient_factory.create_clam() '''披薩店'''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 '''紐約披薩實體店1'''class NYPizzaStore(PizzaStore):  def create_pizza(self, pizza_type):    ingredient_factory = NYPizzaIngredientFactory()     if pizza_type == "cheese":      pizza = CheesePizza(ingredient_factory)      pizza.set_name("紐約風格芝士披薩".decode('utf-8'))    elif pizza_type == "clam":      pizza = ClamPizza(ingredient_factory)      pizza.set_name("紐約風格文蛤披薩".decode('utf-8'))    else:      pizza = None     return pizza '''芝加哥披薩實體店2'''class ChicagoPizzaStore(PizzaStore):   def create_pizza(self, pizza_type):    ingredient_factory = ChicagoPizzaIngredientFactory()     if pizza_type == "cheese":      pizza = CheesePizza(ingredient_factory)      pizza.set_name("芝加哥風格芝士披薩".decode('utf-8'))    elif pizza_type == "clam":      pizza = ClamPizza(ingredient_factory)      pizza.set_name("芝加哥風格文蛤披薩".decode('utf-8'))    else:      pizza = None     return pizza '''生產披薩的工廠'''class PizzaIngredientFactory:  def create_dough(self):    pass   def create_sauce(self):    pass   def create_cheese(self):    pass   def create_clam(self):    pass '''生產披薩的實體工廠1'''class NYPizzaIngredientFactory(PizzaIngredientFactory):  def create_dough(self):    return ThinDough()   def create_sauce(self):    return MarinaraSauce()   def create_cheese(self):    return FreshCheese()   def create_clam(self):    return FreshClam() '''生產披薩的實體工廠2'''class ChicagoPizzaIngredientFactory(PizzaIngredientFactory):  def create_dough(self):    return ThickDough()   def create_sauce(self):    return MushroomSauce()   def create_cheese(self):    return BlueCheese()   def create_clam(self):    return FrozenClam()  class Dough:  def to_string(self):    pass class ThinDough(Dough):  def to_string(self):    return "薄的麵糰" class ThickDough(Dough):  def to_string(self):    return "厚的生麵糰" class Sauce:  def to_string(self):    pass class MarinaraSauce(Sauce):  def to_string(self):    return "番茄醬" class MushroomSauce(Sauce):  def to_string(self):    return "蘑菇醬" class Cheese:  def to_string(self):    pass class FreshCheese(Cheese):  def to_string(self):    return "新鮮的乳酪" class BlueCheese(Cheese):  def to_string(self):    return "藍紋乳酪" class Clam:  def to_string(self):    pass class FreshClam(Clam):  def to_string(self):    return "新鮮的文蛤" class FrozenClam(Clam):  def to_string(self):    return "冷凍的文蛤" if __name__ == "__main__":  # 建立了兩個披薩實體店  ny_store = NYPizzaStore()  chicago_store = ChicagoPizzaStore()   # 在第一個披薩對象中訂購了一個cheese風味的披薩  pizza = ny_store.order_pizza("cheese")  print pizza.to_string()  print "邁克訂購了一個 %s" % pizza.get_name()  print   pizza = chicago_store.order_pizza("clam")  print pizza.to_string()  print "約翰訂購了一個%s" % pizza.get_name()

結果:

準備: 紐約風格芝士披薩烘烤25分鐘在350。切割成對角線切片。放在官方的盒子中。紐約風格芝士披薩:  麵糰: 薄的麵糰  醬汁: 番茄醬  乳酪: 新鮮的乳酪 邁克訂購了一個 紐約風格芝士披薩 準備: 芝加哥風格文蛤披薩烘烤25分鐘在350。切割成對角線切片。放在官方的盒子中。芝加哥風格文蛤披薩:  麵糰: 厚的生麵糰  醬汁: 蘑菇醬  文蛤: 冷凍的文蛤 約翰訂購了一個芝加哥風格文蛤披薩
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.