Пример #1
0
        public void RemoveFromUse(ReusableObject reusableObject)
        {
            inUse.Remove(reusableObject);
            reusableObject.SetDefault();
            inPool.Add(reusableObject);

            reusableObject.isActive = false;

            Console.WriteLine("Object removed from use");
        }
Пример #2
0
 public ReusableObject GetReusableObject()
 {
     if (inPool.Count > 0)
     {
         ReusableObject obj = inPool[0];
         inPool.RemoveAt(0);
         inUse.Add(obj);
         obj.isActive = true;
         return(obj);
     }
     // if no objects are in pool at the moment
     {
         ReusableObject obj = new ReusableObject();
         inUse.Add(obj);
         obj.isActive = true;
         return(obj);
     }
 }
Пример #3
0
        static void Main(string[] args)
        {
            ObjectPool objectPool = new ObjectPool();

            ReusableObject obj1 = objectPool.GetReusableObject();

            obj1.ChangeData("obj1 data");

            ReusableObject obj2 = objectPool.GetReusableObject();

            obj2.ChangeData("obj2 data");

            ReusableObject obj3 = objectPool.GetReusableObject();

            obj3.ChangeData("obj3 data");

            objectPool.RemoveFromUse(obj3);

            ReusableObject obj3_reused = objectPool.GetReusableObject();

            obj3_reused.PrintData();
        }