namespace Chernobyl.DesignPatterns.Pool { /// /// A pool that takes objects from one pool (called the /// "drainage pool") and, if that pool runs out of objects, /// it pulls from another pool (called the "main pool"). /// All returned objects are placed into the "drainage pool" /// instead of the main pool. /// /// The types to pool together. public class DrainagePool : IPool { /// /// Constructor. /// /// The pool to "drain" objects into. /// The pool to pull objects out of if the /// cache dries up. public DrainagePool(IPool drainagePool, IPool mainPool) { DrainingPool = drainagePool; MainPool = mainPool; } /// /// Takes an object from the drainage pool if it still has objects /// left. If no objects are left, then object are taken out /// of the main pool. /// /// The object to fill with /// the taken object. /// True if there was an object that could be /// taken from the pool and that object was assigned to /// , false if otherwise. public bool Take(out PooledType objectToTake) { if (DrainingPool.Take(out objectToTake) == false) { if (MainPool.Take(out objectToTake) == false) return false; } return true; } /// /// Returns a previously taken object to the drainage /// pool even if that object was taken from the main /// pool. /// /// The object to return /// to the pool. public void Return(PooledType objectToReturn) { DrainingPool.Return(objectToReturn); } /// /// Adds an item to the drainage pool. /// /// The item to add to the pool public void Add(PooledType item) { DrainingPool.Add(item); } /// /// Clears out the drainage pool. /// public void Clear() { DrainingPool.Clear(); } /// /// The size of the drainage pool. /// public int Count { get { return DrainingPool.Count; } } /// /// Removes an item from the drainage pool. /// /// The item to remove. /// True if the item was removed, false if /// otherwise. public bool Remove(PooledType item) { return DrainingPool.Remove(item); } /// /// The pool to pull objects from when their /// are objects available. If no objects are /// available then objects are taken from the /// . /// public IPool DrainingPool { get; protected set; } /// /// The pool to take objects from if the cache /// has no more objects left to take. /// public IPool MainPool { get; protected set; } } }