您的位置:首页 > 编程语言 > Python开发

设计模式十四(享元模式flyweight,python语言实现)

2012-12-12 14:03 791 查看
基本理论请参考相关书籍,这里直接给实例



基本解释:享元模式 ,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()


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: