首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Python: Flyweight Pattern

Python: Flyweight Pattern

作者头像
geovindu
发布2026-06-19 09:42:44
发布2026-06-19 09:42:44
220
举报

DuFlyweight.py

代码语言:javascript
复制
# 享元模式 Flyweight Pattern  geovindu,Geovin Du,涂聚文
import time
 
class Arrow:
    def __init__(self, x, y, z, velocity):
        self.x = x
        self.y = y
        self.z = z
        self.velocity = velocity
        self.shape = 'cone'
 
 
class ArrowContext:
    def __init__(self, x, y, z, velocity):
        self.x = x
        self.y = y
        self.z = z
        self.velocity = velocity
 
 
class ArrowFlyweight:
    def __init__(self):
        self.shape = 'cone'
        self.arrows = []
 
    def arrow_factory(self, x, y, z, velocity):
        arr = []
        for b in self.arrows:
            if b.x == x and b.y == y and b.z == z and b.velocity == velocity:
                arr.append(b)
        if not arr:
            arr = Arrow(x, y, z, velocity)
            self.arrows.append(arr)
        else:
            arr = arr[0]
 
        return arr
 
    def print_arrows(self):
        print('排列:')
        for arrow in self.arrows:
            print(str(arrow.x) + ' ' + str(arrow.y) + ' ' + str(arrow.z) + ' ' + str(arrow.velocity))
 
 
# Flyweight Object: HTTP Request result:  geovindu,Geovin Du,涂聚文 格式,排版要对齐,否则调用时,方法到提取不到
class WebRequestResult(object):
   # The time (in munutes, converted to seconds) to cache a request.
   # This is a 1-minute cahce.
   CACHE_TIME_MINUTES = 1 * 60
 
   def __init__(self, data):
       # The Flyweight data:
       self.data = data
       self.cacheUntilTime = time.time() + WebRequestResult.CACHE_TIME_MINUTES
 
   def isExpired(self):
       return time.time() > self.cacheUntilTime
 
# WebRequestCache Factory:
class WebRequestCache(object):
    def __init__(self):
        self.cache = {}
 
    # For simplicity, we'll be concurrent with our requests:
    def getRequest(self, url):
        result = None
 
        if url in self.cache:
            result = self.cache[url]
 
        if result is None or result.isExpired():
            result = WebRequestResult("一些数据来自于 " + str(url))
 
            self.cache[url] = result
 
        return result

main.py  调用

代码语言:javascript
复制
# 享元模式 Flyweight Pattern
 
obj =DuFlyweight.ArrowFlyweight()
obj.arrow_factory(1, 1, 1, 1)
obj.arrow_factory(1, 2, 5, 1)
obj.arrow_factory(1, 1, 1, 1)
obj.print_arrows()
 
geovinduObject =DuFlyweight.ArrowFlyweight()
geovinduObject.arrow_factory('geovindu', 'dusystem.com', '1', '塗聚文')
geovinduObject.arrow_factory('geovin', 'du', '5', '1')
geovinduObject.arrow_factory('geovi', 'studio', '1', '涂聚文')
geovinduObject.print_arrows()
 
webCache = DuFlyweight.WebRequestCache()
 
request = webCache.getRequest("https://coronavirus-tracker-api.herokuapp.com/all")
print(request.data) # output: Some cool data from mycoolapi/endpoint
 
print(request.isExpired()) # output: False

输出:

代码语言:javascript
复制
排列:
1 1 1 1
1 2 5 1
排列:
geovindu dusystem.com 1 塗聚文
geovin du 5 1
geovi studio 1 涂聚文
一些数据来自于 https://coronavirus-tracker-api.herokuapp.com/all
False
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2026-06-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档