2

There's a specific snippet of code that I often use for recycling objects in a list based on a data provider. I thought of making a class to run through the snippet, using a given class to handle each step. This problem is... I don't know what to name it. Does a design pattern exist that describes this reusing of snippets?

package
{
    public class RecycleOperationRunner implements IRecycleOperationRunner
    {
        public function RecycleOperationRunner()
        {

        }

        public function run(operation:IRecycleOperation):void
        {
            const m:int = Math.max(numObjects, numDataItems);

            for (var i:int = 0; i < m; i++)
            {
                if (i < numDataItems)
                {
                    if (i < numObjects)
                    {
                        operation.reuseItem(i);
                    }
                    else
                    {
                        operation.createItem(i);
                    }

                    operation.setupItem(i);
                }
                else
                {
                    operation.removeItem(i);
                }
            }

            operation.dispose();
        }
    }
}
destroytoday
  • 227
  • 2
  • 12

2 Answers2

4

It seems like what you have could be described as a "pool". As in "thread pool" or "connection pool."

It seems to deviate slightly in that typically you request a resource from a pool, and if all the resources are currently being used (leased) then you block until one becomes available. In your example, you create one. So you have a pool that automatically grows in size to be non-blocking.

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
0

Here Martin Fowler describes a Pooling pattern and methods to the situation when all resources are in use and client requests a new one: http://martinfowler.com/bliki/ResourcePool.html

Also see this discussion on implementation: C# Object Pooling Pattern implementation

Community
  • 1
  • 1
ChatGPT
  • 5,334
  • 12
  • 50
  • 69