posts - 929,  comments - 588,  views - 401万
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
       我们首先定义一个对象的基类ObjectPoolBase,用于计数。像这样:
复制代码
 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     }
复制代码

接下来再来写一个池,使用了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     }
复制代码

对于目标对象就继承ObjectPoolBase:

Code

如何使用,看下面代码:
复制代码
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         } 
复制代码
posted on   PetterLiu  阅读(852)  评论(7编辑  收藏  举报
编辑推荐:
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
阅读排行:
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· RFID实践——.NET IoT程序读取高频RFID卡/标签
点击右上角即可分享
微信分享提示