设计模式十四(享元模式flyweight,python语言实现)
2012-12-12 14:03
791 查看
基本理论请参考相关书籍,这里直接给实例
基本解释:享元模式 ,flyweight, 通过对象单元共享技术实现轻量级。
公司信息CompanyInfo 分为 生成信息ProductionInfo和销售信息SalesInfo。
公司老板要求秘书Secretary(享元工厂,这里是信息享元工厂),准备相应信息报表。
如果对应信息报表在秘书处没有存档,则秘书要生成新报表,并在秘书处存档。
如果对应信息报表存在,则秘书直接使用存档信息报表提交给老板。
如果老板感觉信息太旧,可以要求秘书生成当前信息的报表。
运行结果:
基本解释:享元模式 ,flyweight, 通过对象单元共享技术实现轻量级。
公司信息CompanyInfo 分为 生成信息ProductionInfo和销售信息SalesInfo。
公司老板要求秘书Secretary(享元工厂,这里是信息享元工厂),准备相应信息报表。
如果对应信息报表在秘书处没有存档,则秘书要生成新报表,并在秘书处存档。
如果对应信息报表存在,则秘书直接使用存档信息报表提交给老板。
如果老板感觉信息太旧,可以要求秘书生成当前信息的报表。
# -*- coding: utf-8 -*- ####################################################### # # Flyweight.py # Python implementation of the Class Client # Generated by Enterprise Architect # Created on: 12-十二月-2012 12:42:43 # ####################################################### from __future__ import division from __future__ import print_function from __future__ import unicode_literals from future_builtins import * import time class CompanyInfo(object): """This class declares an interface through which flyweights can receive and act on extrinsic state. """ def __init__(self, name="None"): self.name=name +"\n -- 建立时间:" + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) pass def Info(self): print(self.name +"\n -- 使用时间:" + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) ) pass class ProductionInfo(CompanyInfo): """This class declares an interface through which flyweights can receive and act on extrinsic state. """ def __init__(self, name="this is product's information"): super(ProductionInfo,self).__init__(name) pass class SalesInfo(CompanyInfo): """This class declares an interface through which flyweights can receive and act on extrinsic state. """ def __init__(self, name="this is sales' information"): super(SalesInfo,self).__init__(name) pass class Secretary(object): """This class (a) creates and manages flyweight objects, and (b) ensures that flyweights are shared properly. """ m_CompanyInfo= CompanyInfo() def __init__(self): self.dict=dict() pass def GetCurrentInfo(self,key): if self.dict.has_key(key): self.dict.pop(key) pass return self.GetInfo(key) pass def GetInfo(self, key): info=CompanyInfo() if self.dict.has_key(key): info=self.dict[key] pass else: if key=='P': info=ProductionInfo() pass elif key=='S': info=SalesInfo() pass else: info=None pass if info!=None: self.dict[key]=info pass pass return info pass #客户端 class Client: """This class (a) maintains a reference to a flyweight, and (b) computes or stores the extrinsic state of flyweight(s). """ m_SalesInfo= SalesInfo() m_ProductionInfo= ProductionInfo() m_Secretary= Secretary() #老板要求秘书准备公司生产信息报表 info=m_Secretary.GetInfo('P') info.Info() time.sleep(2) #老板要求秘书准备公司销售信息报表 info=m_Secretary.GetInfo('S') info.Info() time.sleep(2) #老板要求秘书准备公司生产信息报表 #此时该报表已经在秘书处备案了,秘书直接取得备案信息 info=m_Secretary.GetInfo('P') info.Info() time.sleep(2) #因为备案的生产信息比较旧,老板要求秘书准备公司现在的生产信息报表 #此时,该报表需要重新生成 info=m_Secretary.GetCurrentInfo('P') info.Info()
运行结果:
相关文章推荐
- 设计模式十八(观察者模式,python语言实现)
- 学习php设计模式 php实现享元模式(flyweight)
- 设计模式十九(中介者模式,python语言实现)
- 学习php设计模式 php实现享元模式(flyweight)
- Java语言设计模式之享元模式(Flyweight)
- 设计模式十(适配器模式,python语言实现)
- 设计模式十七(状态模式,python语言实现)
- 设计模式-享元模式-flyweight-python??
- 设计模式九(单例模式,python语言实现)
- 设计模式十三(外观模式,python语言实现)
- 设计模式八(原型模式,python语言实现)
- 设计模式十二(组合模式,python语言实现)
- 23种设计模式之python实现--Flyweight模式
- 设计模式十一(桥接模式,python 语言实现)
- 设计模式实现(二十二)--- 享元模式(Flyweight)
- 常见设计模式的解析和实现(C++)之十四-Command模式
- 二十三种设计模式及其python实现
- 面向对象设计模式之Flyweight享元模式(结构型)
- [工作中的设计模式]享元模式模式FlyWeight
- Java设计模式----享元模式(FlyWeight)