What are the differences between Flyweight and Object Pool patterns?
What are the differences between Flyweight and Object Pool patterns?
They differ in the way they are used.
Pooled objects can simultaneously be used by a single "client" only. For that, a pooled object must be checked out from the pool, then it can be used by a client, and then the client must return the object back to the pool. Multiple instances of identical objects may exist, up to the maximal capacity of the pool.
In contrast, a Flyweight object is singleton, and it can be used simultaneously by multiple clients.
As for concurrent access, pooled objects can be mutable and they usually don't need to be thread safe, as typically, only one thread is going to use a specific instance at the same time. Flyweight must either be immutable (the best option), or implement thread safety. (Frankly, I'm not sure if a mutable Flyweight is still Flyweight :))
As for performance and scalability, pools can become bottlenecks, if all the pooled objects are in use and more clients need them, threads will become blocked waiting for available object from the pool. This is not the case with Flyweight.
Flyweight vs object pool patterns: When is each useful?
One difference in that flyweights are commonly immutable instances, while resources acquired from the pool usually are mutable.
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.
What are the differences between Flyweight and Object Pool patterns?的更多相关文章
- 设计模式之美:Object Pool(对象池)
索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):实现 DatabaseConnectionPool 类. 实现方式(二):使用对象构造方法和预分配方式实现 ObjectPool ...
- Object Pool
设计模式之美:Object Pool(对象池) 索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):实现 DatabaseConnectionPool 类. 意图 运用对象池化 ...
- .NET Core中Object Pool的简单使用
前言 复用,是一个重要的话题,也是我们日常开发中经常遇到的,不可避免的问题. 举个最为简单,大家最为熟悉的例子,数据库连接池,就是复用数据库连接. 那么复用的意义在那里呢? 简单来说就是减少不必要的资 ...
- Object Pool 对象池的C++11使用(转)
很多系统对资源的访问快捷性及可预测性有严格要求,列入包括网络连接.对象实例.线程和内存.而且还要求解决方案可扩展,能应付存在大量资源的情形. object pool针对特定类型的对象循环利用,这些对象 ...
- 对象池模式(Object Pool Pattern)
本文节选自<设计模式就该这样学> 1 对象池模式的定义 对象池模式(Object Pool Pattern),是创建型设计模式的一种,将对象预先创建并初始化后放入对象池中,对象提供者就能利 ...
- Java小对象的解决之道——对象池(Object Pool)的设计与应用
一.概述 面向对象编程是软件开发中的一项利器,现已经成为大多数编程人员的编程思路.很多高级计算机语言也对这种编程模式提供了很好的支持,例如C++.Object Pascal.Java等.曾经有大量的软 ...
- thinking in object pool
1.背景 对象池为了避免频繁创建耗时或耗资源的大对象,事先在对象池中创建好一定数量的大对象,然后尽量复用对象池中的对象,用户用完大对象之后放回对象池. 2.问题 目前纵观主流语言的实现方式无外乎3个步 ...
- Unity Object Pool完全体
using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; public ...
- 对象池 object pool
对象池适用于: 对象的创建很耗时 对象频繁地释放再创建 对象池的实现:将释放的对象放进对象池中,在新建对象时,从对象池取对象 public class ObjectPool<T> wher ...
随机推荐
- Chrome 扩展
http://www.cnblogs.com/coco1s/p/8004510.html
- wordpress练习
---恢复内容开始--- wp_options数据表储存网站的一些基本信息 bloginfo() //输出 bloginfo('stylesheet_url') //输出style.cs ...
- anconda1.8+cuda9.0+cudnn7.0.5+tensorflow1.7(win10)安装
1.下载安装cuda9.0 https://developer.nvidia.com/cuda-90-download-archive 2.下载cudnn7.0.5,下载cuda9.0的对应版本 ht ...
- php-fpm慢日志配置
upstream timed out (110: Connection timed out) while reading response header from upstream Nginx报错日志 ...
- uvalive 4960 Sensor Network
题意: 给出一个无向图,求一个生成树使得这个生成树的最大边与最小边之差最小,输出这个最小的差值.n的最大值为350. 思路: 这题不看题解想破头也不知道怎么写Orz. 暴力的做法是可以从大到小枚举边作 ...
- RabbitMQ生产者消费者
package com.ra.car.rabbitMQ; import java.io.IOException; import java.util.HashMap; import java.util. ...
- 【Linux学习四】正则表达式
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 一.grep:显示匹配行v:反显示e:使用扩展正则表达式E:不使用正则 ...
- 【Alpha版本】冲刺阶段——Day6
[Alpha版本]冲刺阶段--Day6 阅读目录 今日进展 问题困难 明日任务 今日贡献量 TODOlist [今日进展] 为注册模块增加界面代码 public static void Windows ...
- Git相关整理以及学习
1.取消还未进入暂存区的修改 git checkout + 文件名 2.取消已经进入暂存区但是还未提交的修改 git reset HEAD + 文件名 3.清理在远程仓库已经被删除的本地分支 git ...
- ubuntu16.04 无法连接wifi和校园宽带问题的解决办法
我遇到的问题是在ubuntu16.04系统下无法进行上海大学校园宽带连接或者校园wifi连接,我一个一个来解决这两个问题. 1.无法连接校园宽带的问题:输入校园账号和密码后,宽带始终连接不上.(上海大 ...