.Net中实现简单对象池

       我们首先定义一个对象的基类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         } 
原文地址:https://www.cnblogs.com/wintersun/p/1308650.html