这个设计模式理解起来很容易。百度百科上说的有点绕口。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出处)