举例讲解Python设计模式编程中对抽象工厂模式的运用


Posted in Python onMarch 02, 2016

抽象工厂模式:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
优点:易于交换“产品系列”,只要更改相应的工厂即可。
缺点:建立产品的时候很繁琐,需要增加和修改很多东西。

优化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 = SqlServerFactory() 
  iu = factory.CreateUser() 
  iu.Insert() 
  iu.GetUser() 
  id = factory.CreateDepartment() 
  id.Insert() 
  id.GetUser() 
   
  printInfo("\n--抽象工厂方法+简单工厂方法--") 
  iu = DataAccess.CreateUser() 
  iu.Insert() 
  iu.GetUser() 
  id = DataAccess.CreateDepartment() 
  id.Insert() 
  id.GetUser() 
   
  printInfo("\n-抽象工厂方法+简单工厂方法+反射-") 
  iu = DataAccessPro.CreateUser() 
  iu.Insert() 
  iu.GetUser() 
  id = DataAccessPro.CreateDepartment() 
  id.Insert() 
  id.GetUser()   
  return 
 
 
if __name__ == '__main__': 
  clientUI();

类图:

举例讲解Python设计模式编程中对抽象工厂模式的运用

工厂模式和抽象工厂模式的区别:工厂模式是在派生类中定义一个工厂的抽象接口,然后基类负责创建具体对象;抽象工厂模式是维护一个产品家族,由基类定义产品被生产的方法,客户根据派生类的接口进行开发。

实例:人民群众喜闻乐见的披萨店例子这里又可以搬出来了,这次我们根据抽象工厂模式的特点,用不同原材料制作不同口味的披萨,创建不同原材料的工厂,不同实体店做出口味不同的披萨。创建一个产品家族(Dough、Sauce、Cheese和Clam)的抽象类型(PizzaIngredientFactory),这个类型的子类(NYPizzaIngredientFactory和ChicagoPizzaIngredientFactory)定义了产品被产生的方法。
代码:

#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
reload(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。
切割成对角线切片。
放在官方的盒子中。
芝加哥风格文蛤披萨:
  面团: 厚的生面团
  酱汁: 蘑菇酱
  文蛤: 冷冻的文蛤
 
约翰订购了一个芝加哥风格文蛤披萨
Python 相关文章推荐
python判断、获取一张图片主色调的2个实例
Apr 10 Python
简单实现python收发邮件功能
Jan 05 Python
对python中raw_input()和input()的用法详解
Apr 22 Python
用Python分析3天破10亿的《我不是药神》到底神在哪?
Jul 12 Python
Python中常用的内置方法
Jan 28 Python
Python实现将字符串的首字母变为大写,其余都变为小写的方法
Jun 11 Python
Python三元运算与lambda表达式实例解析
Nov 30 Python
tensorflow之并行读入数据详解
Feb 05 Python
Python中内建模块collections如何使用
May 27 Python
Numpy中np.random.rand()和np.random.randn() 用法和区别详解
Oct 23 Python
使用AJAX和Django获取数据的方法实例
Oct 25 Python
python机器学习Github已达8.9Kstars模型解释器LIME
Nov 23 Python
实例讲解Python设计模式编程之工厂方法模式的使用
Mar 02 #Python
详解设计模式中的工厂方法模式在Python程序中的运用
Mar 02 #Python
探究python中open函数的使用
Mar 01 #Python
Saltstack快速入门简单汇总
Mar 01 #Python
通过Python使用saltstack生成服务器资产清单
Mar 01 #Python
Python解析json文件相关知识学习
Mar 01 #Python
使用简单工厂模式来进行Python的设计模式编程
Mar 01 #Python
You might like
PHP屏蔽蜘蛛访问代码及常用搜索引擎的HTTP_USER_AGENT
2013/03/06 PHP
hadoop中一些常用的命令介绍
2013/06/19 PHP
PHP随机字符串生成代码(包括大小写字母)
2013/06/24 PHP
php实现的百度搜索某地天气的小偷代码
2014/04/23 PHP
PHP循环遍历数组的3种方法list()、each()和while总结
2014/11/19 PHP
PHP响应post请求上传文件的方法
2015/12/17 PHP
Yii2.0多文件上传实例说明
2017/07/24 PHP
PHP学习笔记之session
2018/05/06 PHP
jquery中map函数与each函数的区别实例介绍
2014/06/23 Javascript
js全选实现和判断是否有复选框选中的方法
2015/02/17 Javascript
基于jQuery实现带动画效果超炫酷的弹出对话框(附源码下载)
2016/02/22 Javascript
基于jQuery实现滚动刷新效果
2017/01/09 Javascript
Vue动态组件实例解析
2017/08/20 Javascript
微信小程序使用audio组件播放音乐功能示例【附源码下载】
2017/12/08 Javascript
vue2 前端搜索实现示例
2018/02/26 Javascript
详解vue组件开发脚手架
2018/06/15 Javascript
Vue CLI3中使用compass normalize的方法
2019/05/30 Javascript
Vue实现商品分类菜单数量提示功能
2019/07/26 Javascript
[02:19]DOTA2上海特级锦标赛 观赛指南 Spectator Guide
2016/02/04 DOTA
[00:32]2018DOTA2亚洲邀请赛出场——LGD
2018/04/04 DOTA
python使用tensorflow保存、加载和使用模型的方法
2018/01/31 Python
基于python OpenCV实现动态人脸检测
2018/05/25 Python
Python初学者常见错误详解
2019/07/02 Python
一文秒懂python读写csv xml json文件各种骚操作
2019/07/04 Python
我们为什么要减少Python中循环的使用
2019/07/10 Python
通过python实现弹窗广告拦截过程详解
2019/07/10 Python
利用SVG和CSS3来实现一个炫酷的边框动画
2015/07/22 HTML / CSS
美国领先的眼镜和太阳镜在线零售商:Glasses.com
2019/08/26 全球购物
超市端午节活动方案
2014/01/23 职场文书
社区服务活动小结
2014/07/08 职场文书
学校禁毒宣传活动总结
2015/05/08 职场文书
教导处教学工作总结
2015/08/12 职场文书
2016感恩母亲节校园广播稿
2015/12/17 职场文书
pycharm debug 断点调试心得分享
2021/04/16 Python
nginx配置虚拟主机的详细步骤
2021/07/21 Servers
mybatis 获取更新记录的id
2022/05/20 Java/Android