這個設計模式理解起來很容易。百度百科上說的有點繞口。html
運用共享技術來有効地支持大量細粒度對象的複用。java
它經過共享已經存在的對橡大幅度減小須要建立的對象數量、避免大量類似類的開銷,從而提升系統資源的利用率。
python
相同對象只要保存一份,這下降了系統中對象的數量,從而下降了系統中細粒度對象給內存帶來的壓力。
git
1.爲了共享對象,須要將不能共享的狀態外部化,會增長程序的複雜性github
2.對享元模式的外部狀態會增加運行時間數據庫
1.內部狀態,不會隨着環境的改變而改變,可共享的部分設計模式
2.外部狀態,會隨着環境的改變而改變,是不可共享的部分.app
享元模式就是要區分這兩種狀態,並將外部狀態外部化。dom
1.java中的String,若是有則返回,若是沒有就建立一個字符串保存在字符串緩衝池裏面。.net
2.數據庫的數據池
1.系統中存在大量的類似對象
2.細粒度的對象都具有較接近的外部狀態,並且內部狀態與環境無關,也就是說對象沒有特定身份
3.須要緩衝池的場景
抽象享元角色(Flyweight):是全部的具體享元類的基類,爲具體享元規範須要實現的公共接口,非享元的外部狀態以參數的形式經過方法傳入。
具體享元(Concrete Flyweight)角色:實現抽象享元角色中所規定的接口。
非享元(Unsharable Flyweight)角色:是不能夠共享的外部狀態,它以參數的形式注入具體享元的相關方法中。
享元工廠(Flyweight Factory)角色:負責建立和管理享元角色。當客戶對象請求一個享元對象時,享元工廠檢査系統中是否存在符合要求的享元對象,若是存在則提供給客戶;若是不存在的話,則建立一個新的享元對象。
舉下圍棋的例子
棋子:是抽象享元角色
白子:具體享元角色
黑子:具體享元角色
圍棋工廠:是享元工廠角色
下棋的位置:非具體享元角色
class Flyweight(object): def __init__(self, str): self.str = str def display(self): print("show the string: " + self.str) class FlyweightFactory(object): def __init__(self): self.flyweights = {} def getFlyweight(self, obj): flyweight = self.flyweights.get(obj) if flyweight == None: flyweight = Flyweight(str(obj)) self.flyweights[obj] = flyweight return flyweight def showFlyweights(self): for i in self.flyweights: self.flyweights[i].display() print(len(self.flyweights)) if __name__ == "__main__": flyweightfactory = FlyweightFactory() fly1 = flyweightfactory.getFlyweight("hello1") fly2 = flyweightfactory.getFlyweight("hello1") fly3 = flyweightfactory.getFlyweight("hello2") fly4 = flyweightfactory.getFlyweight("hello2") fly5 = flyweightfactory.getFlyweight("hello3") flyweightfactory.showFlyweights()
out:
show the string: hello2
show the string: hello1
show the string: hello3
3
import random from enum import Enum TreeType=Enum("TreeType",("apple","cherry","peach")) class Tree: pool={} #數目池 def __new__(cls,tree_type): obj=cls.pool.get(tree_type,None) #獲取樹苗類型 if not obj: #若是以前沒有建立過 obj=object.__new__(cls) #開闢一塊內存空間 cls.pool[tree_type]=obj #在池子裏添加這個樹苗類型和他的內存空間 obj.tree_type=tree_type #內存空間裏添加樹苗類型 return obj #返回樹苗類型空間 def render(self,age,x,y): print("建立了一個新的種類{}的樹苗,他的年齡是{},地點位於{},{}".format( self.tree_type,age,x,y )) def main(): rnd=random.Random() age_min,age_max=1,30 #樹苗年齡在1-30之間隨機 min_point,max_point=0,100 #隨機地點 tree_counter=0 print(rnd) for _ in range(10): #10個蘋果樹 t1=Tree(TreeType['apple']) t1.render(rnd.randint(age_min,age_max), rnd.randint(min_point,max_point), rnd.randint(min_point, max_point)) tree_counter+=1 for _ in range(3): #3個cherry樹 t2=Tree(TreeType['cherry']) t2.render(rnd.randint(age_min,age_max), rnd.randint(min_point,max_point), rnd.randint(min_point, max_point)) tree_counter+=1 for _ in range(5): #5個peach樹 t3=Tree(TreeType['peach']) t3.render(rnd.randint(age_min,age_max), rnd.randint(min_point,max_point), rnd.randint(min_point, max_point)) tree_counter+=1 print("樹苗建立了{}個".format(tree_counter)) print("樹苗完成建立:{}種類".format(len(Tree.pool))) t4=Tree(TreeType['cherry']) t5 = Tree(TreeType['cherry']) t6=Tree(TreeType['apple']) print("{}----{}是同一顆樹嗎? {}".format(id(t4),id(t5),id(t4)==id(t5))) print("{}----{}是同一顆樹嗎? {}".format(id(t5), id(t6), id(t6) == id(t5))) if __name__ == '__main__': main()
http://c.biancheng.net/view/1371.html(下圍棋)
https://www.runoob.com/design-pattern/flyweight-pattern.html
https://www.cnblogs.com/hujingnb/p/10171607.html
http://dongweiming.github.io/python-flyweight.html
https://blog.csdn.net/weixin_42557907/article/details/84204040(例子2出處)
https://blog.csdn.net/u013346751/article/details/78426104(例子1出處)