destroytoday
destroytoday

Reputation: 227

Is there a name for this design pattern?

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();
        }
    }
}

Upvotes: 2

Views: 341

Answers (2)

ChatGPT
ChatGPT

Reputation: 5617

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

Upvotes: 0

Mark Peters
Mark Peters

Reputation: 81054

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.

Upvotes: 4

Related Questions