Tuesday, August 18, 2009

Object Pool Design Pattern in C#




Intent: The intent of object pool is to create a pool object to reduce the load of creating objects.

Object pooling is nothing but creation of limited amount of objects in memory and reuse then instead of creating a new one. This can be achieved with the help of a pool manager. The pool manager knows the number of maximum instances to be created for an object type. Once the maximum number is reached pool manager puts them in to an object container and all new objects requests are being served from the object container.

Like Lazy Initialization object pooling can be achieved be either using Singleton or Factory Method.

Benefits:

The main advantage of using the Lazy initialization is that it will not consume unnecessary expansive resources for creating the object until it is required for the first time. Also if the object is already created the request is being served from the object created instead of creating a new object.

Software Example & Explanation of Code:

Let us assume that one of our code requirements is to create Employee objects. We have maximum restriction of two objects and anything more than two requests has to be served from the queue. For our example we will use Factory Method. We will be using the “Queue” class of “System.Collections” namespace for pooling our objects.

Note: Employee may not be good example when you think from software prospective, but this definitely helps you in understanding the concept.

EmployeeFactory.cs

using System;
using System.Collections;

namespace ObjectPooling
{
public class EmployeeFactory
{
//Defining Max pool size of Objects as two
static int _intMaxPoolSize = 2;

//Collection class to hold objects
static readonly Queue objPool = new Queue(_intMaxPoolSize);

//Creates new employee only when the count of objects is more than two
//Otherwise served from pool
public Employee GetEmployee()
{
Employee objEmployee;
if (Employee._intCounter >= 2 && objPool.Count > 1)
{
objEmployee = GetEmployeeFromPool();
}
else
{
objEmployee = CreateNewEmployee();
}
return objEmployee;
}

private Employee GetEmployeeFromPool()
{
Employee objEmp;
if (objPool.Count > 1)
{
objEmp = (Employee)objPool.Dequeue();
Employee._intCounter--;
}
else
{
objEmp = new Employee();
}
return objEmp;
}
private Employee CreateNewEmployee()
{
Employee objEmp = new Employee();
objPool.Enqueue(objEmp);
return objEmp;
}
}
}



Employee.cs

using System;

namespace ObjectPooling
{
public class Employee
{
public static int _intCounter = 0;

public Employee()
{
_intCounter = _intCounter + 1;
_FirstName = "FirstName" + _intCounter.ToString();
_LastName = "LastName" + _intCounter.ToString();
}

private string _FirstName;

public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
private string _LastName;

public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}
}
}

Default.aspx.cs

protected void btnCreate_Click(object sender, EventArgs e)
{
EmployeeFactory objEFact = new EmployeeFactory();

Employee obj1 = objEFact.GetEmployee();
Employee obj2 = objEFact.GetEmployee();
Employee obj3 = objEFact.GetEmployee();
Employee obj4 = objEFact.GetEmployee();

Response.Write("Object 1 Values are-----" + obj1.FirstName +"--"+obj1.LastName+"--");
Response.Write("Object 2 Values are-----" + obj2.FirstName +"--"+obj2.LastName+"--");
Response.Write("Object 3 Values are-----" + obj3.FirstName +"--"+obj3.LastName+"--");
Response.Write("Object 4 Values are-----" + obj4.FirstName + "--" + obj4.LastName + "--");
}


Here in the above code in the button click we are trying to create four object of employee class. When the code runs it initially creates two objects and other two create request are being served from the queue.

The entire code for download is available at http://www.csharppatterns.com/

No comments: