我们首先定义一个对象的基类ObjectPoolBase,用于计数。像这样:
接下来再来写一个池,使用了Queue<T>,像这样
Code
如何使用,看下面代码:
1 /// <summary>
2 /// ObjectPoolBase
3 /// </summary>
4 /// <remarks>Author Petter Liu http://wintersun.cnblogs.com </remarks>
5 public class ObjectPoolBase
6 {
7 /// <summary>
8 /// ObjectPoolBase Counter
9 /// </summary>
10 public static int Counter = 0;
11
12 /// <summary>
13 /// Initializes a new instance of the <see cref="ObjectPoolBase"/> class.
14 /// </summary>
15 public ObjectPoolBase()
16 {
17 ++Counter;
18 }
19 }
2 /// ObjectPoolBase
3 /// </summary>
4 /// <remarks>Author Petter Liu http://wintersun.cnblogs.com </remarks>
5 public class ObjectPoolBase
6 {
7 /// <summary>
8 /// ObjectPoolBase Counter
9 /// </summary>
10 public static int Counter = 0;
11
12 /// <summary>
13 /// Initializes a new instance of the <see cref="ObjectPoolBase"/> class.
14 /// </summary>
15 public ObjectPoolBase()
16 {
17 ++Counter;
18 }
19 }
接下来再来写一个池,使用了Queue<T>,像这样
1 /// <summary>
2 /// ObjectPool Generic Verison
3 /// <seealso cref="http://www.c-sharpcorner.com/UploadFile/vmsanthosh.chn/109042007094154AM/1.aspx"/>
4 /// <remarks>Author Petter Liu http://wintersun.cnblogs.com </remarks>
5 /// </summary>
6 /// <typeparam name="T">Need to Object</typeparam>
7 public class ObjectPoolFactory<T> where T : ObjectPoolBase, new()
8 {
9 // Maximum objects allowed!
10 private static int _PoolMaxSize = 2;
11
12 // My Collection Pool
13 private static readonly Queue<T> objPool = new Queue<T>(_PoolMaxSize);
14
15 /// <summary>
16 /// Gets the object from pool.
17 /// </summary>
18 /// <returns>ObjectPoolBase</returns>
19 public ObjectPoolBase GetObjectFromPool<T>()
20 {
21 ObjectPoolBase objectt = null;
22 // check from the collection pool. If exists return object else create new
23 if (ObjectPoolBase.Counter >= _PoolMaxSize && objPool.Count > 0)
24 {
25 // Retrieve from pool
26 objectt = RetrieveFromPool<T>();
27 }
28 else
29 {
30 objectt = GetNewObject();
31 }
32 return objectt;
33 }
34
35 /// <summary>
36 /// Gets the new object.
37 /// </summary>
38 /// <returns>ObjectPoolBase</returns>
39 private ObjectPoolBase GetNewObject()
40 {
41 // Creates a new object
42 T t1 = new T();
43 objPool.Enqueue(t1);
44 return t1;
45 }
46
47 /// <summary>
48 /// Retrieves from pool.
49 /// </summary>
50 /// <returns>ObjectPoolBase</returns>
51 protected ObjectPoolBase RetrieveFromPool<T>()
52 {
53 ObjectPoolBase object1;
54 // if there is any objects in my collection
55 if (objPool.Count > 0)
56 {
57 object1 = (ObjectPoolBase)objPool.Dequeue();
58 ObjectPoolBase.Counter--;
59 }
60 else
61 {
62 // return a new object
63 object1 = new ObjectPoolBase();
64 }
65 return object1;
66 }
67 }
2 /// ObjectPool Generic Verison
3 /// <seealso cref="http://www.c-sharpcorner.com/UploadFile/vmsanthosh.chn/109042007094154AM/1.aspx"/>
4 /// <remarks>Author Petter Liu http://wintersun.cnblogs.com </remarks>
5 /// </summary>
6 /// <typeparam name="T">Need to Object</typeparam>
7 public class ObjectPoolFactory<T> where T : ObjectPoolBase, new()
8 {
9 // Maximum objects allowed!
10 private static int _PoolMaxSize = 2;
11
12 // My Collection Pool
13 private static readonly Queue<T> objPool = new Queue<T>(_PoolMaxSize);
14
15 /// <summary>
16 /// Gets the object from pool.
17 /// </summary>
18 /// <returns>ObjectPoolBase</returns>
19 public ObjectPoolBase GetObjectFromPool<T>()
20 {
21 ObjectPoolBase objectt = null;
22 // check from the collection pool. If exists return object else create new
23 if (ObjectPoolBase.Counter >= _PoolMaxSize && objPool.Count > 0)
24 {
25 // Retrieve from pool
26 objectt = RetrieveFromPool<T>();
27 }
28 else
29 {
30 objectt = GetNewObject();
31 }
32 return objectt;
33 }
34
35 /// <summary>
36 /// Gets the new object.
37 /// </summary>
38 /// <returns>ObjectPoolBase</returns>
39 private ObjectPoolBase GetNewObject()
40 {
41 // Creates a new object
42 T t1 = new T();
43 objPool.Enqueue(t1);
44 return t1;
45 }
46
47 /// <summary>
48 /// Retrieves from pool.
49 /// </summary>
50 /// <returns>ObjectPoolBase</returns>
51 protected ObjectPoolBase RetrieveFromPool<T>()
52 {
53 ObjectPoolBase object1;
54 // if there is any objects in my collection
55 if (objPool.Count > 0)
56 {
57 object1 = (ObjectPoolBase)objPool.Dequeue();
58 ObjectPoolBase.Counter--;
59 }
60 else
61 {
62 // return a new object
63 object1 = new ObjectPoolBase();
64 }
65 return object1;
66 }
67 }
对于目标对象就继承ObjectPoolBase:

如何使用,看下面代码:
1 [Test]
2 public void ObjectPoolFactoryForGeneric()
3 {
4 ObjectPoolFactory<Parent> poo = new ObjectPoolFactory<Parent>();
5 Parent Parent1 = poo.GetObjectFromPool<Parent>() as Parent;
6 Console.WriteLine("First object");
7 Parent Parent2 = poo.GetObjectFromPool<Parent>() as Parent;
8 Console.WriteLine("Second object");
9 }
2 public void ObjectPoolFactoryForGeneric()
3 {
4 ObjectPoolFactory<Parent> poo = new ObjectPoolFactory<Parent>();
5 Parent Parent1 = poo.GetObjectFromPool<Parent>() as Parent;
6 Console.WriteLine("First object");
7 Parent Parent2 = poo.GetObjectFromPool<Parent>() as Parent;
8 Console.WriteLine("Second object");
9 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· RFID实践——.NET IoT程序读取高频RFID卡/标签