Pool.cs 523 B

1234567891011121314151617181920212223242526272829
  1. using System.Collections.Generic;
  2. namespace ETModel
  3. {
  4. public class Pool<T> where T: class, new()
  5. {
  6. private readonly Queue<T> pool = new Queue<T>();
  7. public T Fetch()
  8. {
  9. if (pool.Count == 0)
  10. {
  11. return new T();
  12. }
  13. return pool.Dequeue();
  14. }
  15. public void Recycle(T t)
  16. {
  17. pool.Enqueue(t);
  18. }
  19. public void Clear()
  20. {
  21. this.pool.Clear();
  22. }
  23. }
  24. }