Flyweight vs对象池模式:什么时候有用? [英] Flyweight vs object pool patterns: When is each useful?

查看:140
本文介绍了Flyweight vs对象池模式:什么时候有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,对象池是一个创意模式,而flyweight是一个结构模式,但实际上我看不出这两种模式之间的差异,有人可以向我解释一下差异,每个都可以在实现中有用吗?

As far as I know the object pool is a creational pattern and the flyweight is a structural pattern, but actually I can´t see very much difference between that two patterns, could someone please explain to me the difference and when each could be useful in an implementation?

推荐答案

该flyweights的一个区别是通常是不可变的实例,而从池中获取的资源通常是可变的

One difference in that flyweights are commonly immutable instances, while resources acquired from the pool usually are mutable.

所以你创建flyweights,以避免重复创建包含相同状态的对象的多个实例的成本(因为它们完全相同,只创建一个并重用它在您的应用程序的所有地方),而池中的资源是您要单独控制的特定资源,并且可能具有不同的状态,但是您不想支付创建和销毁的成本,因为它们都在相同的状态。

So you create flyweights to avoid the cost of repeatedly create multiple instances of objects containing the same state (because they are all the same, you just create only one and reuse it throughout all places in your app), while resources in a pool are particular resources that you want to control individually and possibly have different state, but you don't want to pay the cost of creation and destruction because they are all initialized in the same state.

这篇关于Flyweight vs对象池模式:什么时候有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆