123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using System;
- using System.Collections.Generic;
- namespace ET
- {
- public class ObjectPool: Singleton<ObjectPool>
- {
- private readonly Dictionary<Type, Queue<object>> pool = new Dictionary<Type, Queue<object>>();
-
- public T Fetch<T>() where T: class
- {
- return this.Fetch(typeof (T)) as T;
- }
- public object Fetch(Type type)
- {
- Queue<object> queue = null;
- if (!pool.TryGetValue(type, out queue))
- {
- return Activator.CreateInstance(type);
- }
- if (queue.Count == 0)
- {
- return Activator.CreateInstance(type);
- }
- return queue.Dequeue();
- }
- public void Recycle(object obj)
- {
- Type type = obj.GetType();
- Queue<object> queue = null;
- if (!pool.TryGetValue(type, out queue))
- {
- queue = new Queue<object>();
- pool.Add(type, queue);
- }
-
-
- queue.Enqueue(obj);
- }
- }
- }
|