Python设计模式之享元模式原理与用法实例分析
作者:Andy冉明 时间:2021-08-20 20:01:57
本文实例讲述了Python设计模式之享元模式原理与用法。分享给大家供大家参考,具体如下:
享元模式(Flyweight Pattern):运用共享技术有效地支持大量细粒度的对象.
下面是一个享元模式的demo:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Andy'
"""
大话设计模式
设计模式——享元模式
享元模式(Flyweight Pattern):运用共享技术有效地支持大量细粒度的对象
对一个类进行的实例,只在第一次使用时建立,其他时候是用同一个实例,减少内存的开销
"""
# 抽象网站类
class Website(object):
def use(self):
pass
# 具体网站类
class ConcreteWebsite(Website):
def __init__(self, name):
self.name = name
def use(self):
print "网站分类",self.name
# 不共享的网站类
class UnshareConcreteWebsite(Website):
def __init__(self, name):
self.name = name
def use(self):
print "不共享网站分类",self.name
# 网站工厂
class WebsiteFactory(object):
def __init__(self):
self.hashtable = dict()
# 获取网站类 如果存在直接返回,如果不存在建好之后返回
def get_website(self, key):
if not key in self.hashtable:
self.hashtable[key] = ConcreteWebsite(key)
return self.hashtable[key]
# 网站实例的个数
def get_website_count(self):
return len(self.hashtable.keys())
if __name__ == "__main__":
factory = WebsiteFactory()
f1 = factory.get_website("blog")
f2 = factory.get_website("blog")
f3 = factory.get_website("blog")
f4 = factory.get_website("website")
f5 = factory.get_website("website")
f6 = factory.get_website("website")
f7 = UnshareConcreteWebsite("test")
f1.use()
f2.use()
f3.use()
f4.use()
f5.use()
f6.use()
f7.use()
运行结果:
上面类的设计如下图:
享元模式可以避免大量非常相似类的开销,在程序设计中,有时会生成大量细粒度的类实例来表示数据,如果这些实例除了几个参数外基本相同,就可以把参数已到实例外面,在方法调用时,把它们传进来,就可以通过共享大幅度减少单个实例的数目
希望本文所述对大家Python程序设计有所帮助。
来源:https://www.cnblogs.com/onepiece-andy/p/python-flyweight-pattern.html
标签:Python,设计模式,享元模式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python可视化大屏库big_screen示例详解
2021-10-16 14:32:39
![](https://img.aspxhome.com/file/2023/7/87487_0s.jpg)
Python-jenkins模块获取jobs的执行状态操作
2022-07-29 14:15:09
![](https://img.aspxhome.com/file/2023/9/70809_0s.jpg)
如何通过PHP实现Des加密算法代码实例
2023-10-08 09:41:55
Python的numpy库下的几个小函数的用法(小结)
2021-12-13 10:29:33
Selenium+BeautifulSoup+json获取Script标签内的json数据
2023-06-17 09:30:18
python爬虫实现POST request payload形式的请求
2023-11-07 21:08:55
![](https://img.aspxhome.com/file/2023/0/63880_0s.jpg)
Python Websocket服务端通信的使用示例
2021-09-16 15:03:19
![](https://img.aspxhome.com/file/2023/8/110018_0s.png)
Python实现爬虫抓取与读写、追加到excel文件操作示例
2023-11-10 09:08:34
擦除式图片轮番显示效果
2013-08-10 11:01:48
selenium使用chrome浏览器测试(附chromedriver与chrome的对应关系表)
2022-08-16 16:42:00
![](https://img.aspxhome.com/file/2023/0/98930_0s.png)
CSS 的模块化思想
2009-02-03 12:52:00
Python cookbook(数据结构与算法)保存最后N个元素的方法
2023-08-08 05:06:38
![](https://img.aspxhome.com/file/2023/9/62279_0s.png)
让验证码友好一点
2007-10-20 13:45:00
![](https://img.aspxhome.com/file/UploadPic/200710/20/20071020135017833s.gif)
Frontpage2003的怪bug,自动添加“../”的父级目录
2007-09-30 13:30:00
![](https://img.aspxhome.com/file/UploadPic/20079/30/2007930133147256s.jpg)
Python脚本实现自动发带图的微博
2021-04-18 08:37:01
![](https://img.aspxhome.com/file/2023/4/100264_0s.png)
python负载均衡的简单实现方法
2021-10-31 23:15:22
Oracle数据操作和控制语言详解
2008-01-16 19:18:00
Python函数any()和all()的用法及区别介绍
2021-11-23 04:43:16
Python ZipFile模块详解
2021-09-17 06:30:24
基于python实现高速视频传输程序
2022-04-01 02:14:17