| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using System;
- using System.Collections.Generic;
- namespace ETModel
- {
- public class ObjectPool
- {
- private readonly Dictionary<Type, Queue<Component>> dictionary = new Dictionary<Type, Queue<Component>>();
- public Component Fetch(Type type)
- {
- Queue<Component> queue;
- if (!this.dictionary.TryGetValue(type, out queue))
- {
- queue = new Queue<Component>();
- this.dictionary.Add(type, queue);
- }
- Component obj;
- if (queue.Count > 0)
- {
- obj = queue.Dequeue();
- }
- else
- {
- obj = (Component)Activator.CreateInstance(type);
- }
- obj.IsFromPool = true;
- return obj;
- }
- public T Fetch<T>() where T: Component
- {
- T t = (T) this.Fetch(typeof(T));
- return t;
- }
-
- public void Recycle(Component obj)
- {
- Type type = obj.GetType();
- Queue<Component> queue;
- if (!this.dictionary.TryGetValue(type, out queue))
- {
- queue = new Queue<Component>();
- this.dictionary.Add(type, queue);
- }
- queue.Enqueue(obj);
- }
- public void Clear()
- {
- foreach (var kv in this.dictionary)
- {
- foreach (Component component in kv.Value)
- {
- component.IsFromPool = false;
- component.Dispose();
- }
- }
- this.dictionary.Clear();
- }
- }
- }
|