Object Pool
1 public class Pool<T> where T : class, new()
2 {
3 Static Members#region Static Members
4 private int ITEM_COUNT_FOR_ONCE = 10;
5 private int DEFAULT_MAX_CAPACITY = 100;
6 #endregion
7
8 Private Members#region Private Members
9 private LinkedList<T> mFreeList = new LinkedList<T>();
10 private List<T> mUsedList = new List<T>();
11 #endregion
12
13 Constructors / Destructors#region Constructors / Destructors
14 public Pool()
15 {
16 }
17 #endregion
18
19 Manipulators#region Manipulators
20 public Func<T> Allocator
21 {
22 get;
23 set;
24 }
25
26 public Action<T> Initializer
27 {
28 get;
29 set;
30 }
31
32 public Action<T> Terminator
33 {
34 get;
35 set;
36 }
37
38 public T Acquire()
39 {
40 EnsureCapacity();
41
42 // Todo: need to consider the max capacity?
43
44 // Get the first item
45 T item = mFreeList.First.Value;
46 mFreeList.RemoveFirst();
47 mUsedList.Add(item);
48
49 // Delegate to do some initialize works
50 if (Initializer != null)
51 Initializer(item);
52
53 return item;
54 }
55
56 public void Release(T item)
57 {
58 // Remove it
59 System.Diagnostics.Debug.Assert(mUsedList.Contains(item));
60 mUsedList.Remove(item);
61 mFreeList.AddLast(item);
62
63 // Delegate to do some terminate works
64 if (Terminator != null)
65 Terminator(item);
66 }
67
68 #endregion
69
70 Implement Details#region Implement Details
71 private void EnsureCapacity()
72 {
73 if (mFreeList.Count == 0)
74 AllocateMoreItems();
75 }
76
77 private void AllocateMoreItems()
78 {
79 for (int i = 0; i < ITEM_COUNT_FOR_ONCE; i++)
80 {
81 T item = AllocateItem();
82
83 mFreeList.AddLast(item);
84 }
85 }
86
87 private T AllocateItem()
88 {
89 T item = default(T);
90 if (Allocator == null)
91 item = new T();
92 else
93 item = Allocator(); // Delegate to allocate item
94
95 return item;
96 }
97 #endregion
98 }
99
1 public class Pool<T> where T : class, new()
2 {
3 Static Members#region Static Members
4 private int ITEM_COUNT_FOR_ONCE = 10;
5 private int DEFAULT_MAX_CAPACITY = 100;
6 #endregion
7
8 Private Members#region Private Members
9 private LinkedList<T> mFreeList = new LinkedList<T>();
10 private List<T> mUsedList = new List<T>();
11 #endregion
12
13 Constructors / Destructors#region Constructors / Destructors
14 public Pool()
15 {
16 }
17 #endregion
18
19 Manipulators#region Manipulators
20 public Func<T> Allocator
21 {
22 get;
23 set;
24 }
25
26 public Action<T> Initializer
27 {
28 get;
29 set;
30 }
31
32 public Action<T> Terminator
33 {
34 get;
35 set;
36 }
37
38 public T Acquire()
39 {
40 EnsureCapacity();
41
42 // Todo: need to consider the max capacity?
43
44 // Get the first item
45 T item = mFreeList.First.Value;
46 mFreeList.RemoveFirst();
47 mUsedList.Add(item);
48
49 // Delegate to do some initialize works
50 if (Initializer != null)
51 Initializer(item);
52
53 return item;
54 }
55
56 public void Release(T item)
57 {
58 // Remove it
59 System.Diagnostics.Debug.Assert(mUsedList.Contains(item));
60 mUsedList.Remove(item);
61 mFreeList.AddLast(item);
62
63 // Delegate to do some terminate works
64 if (Terminator != null)
65 Terminator(item);
66 }
67
68 #endregion
69
70 Implement Details#region Implement Details
71 private void EnsureCapacity()
72 {
73 if (mFreeList.Count == 0)
74 AllocateMoreItems();
75 }
76
77 private void AllocateMoreItems()
78 {
79 for (int i = 0; i < ITEM_COUNT_FOR_ONCE; i++)
80 {
81 T item = AllocateItem();
82
83 mFreeList.AddLast(item);
84 }
85 }
86
87 private T AllocateItem()
88 {
89 T item = default(T);
90 if (Allocator == null)
91 item = new T();
92 else
93 item = Allocator(); // Delegate to allocate item
94
95 return item;
96 }
97 #endregion
98 }
99