Object Pool Design Pattern

Author - Webner
29.12.2021
|
0 Comments
||

Definition

While building applications, there are objects that are expensive to build in terms of the system resources that they require. Creating such objects, again and again, will impact the overall performance of the application. To prevent the overall work of creating new objects, again and again, the object pool design pattern comes into play.
So, an object-pool is like a container that provides a caching mechanism for the object so that system performance can be enhanced.
All the objects that are not currently in use are placed in a container and can be implemented using a data structure like a queue, which makes enqueue and dequeue easy.
We can configure the maximum and minimum numbers of objects allowed in a pool.

Example Illustration:-

This example demonstrates a Checker Class having a queue. This will check the queue for objects, if objects are present in the pool, then the it will get returned otherwise a new object will be created.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using System.Collections;
namespace ObjectPool
{
class Pooling
{
static void Main(string[] args)
{
Checker fa = new Checker();
AssignObj myObj = fa.GetAssignObj();
Console.WriteLine("1st object");
AssignObj myObj1 = fa.GetAssignObj();
Console.WriteLine("2nd object");
AssignObj myObj2 = fa.GetAssignObj();
Console.WriteLine("3rd object");
Console.Read();
}
}
class Checker
{
// Max objects allowed
private static int max = 3;
// Pool Collection
private static readonly Queue PoolStore = new
Queue(max);
public AssignObj GetAssignObj()
{
AssignObj oAssignObj;
// Check from the pool collection. If object exists, return object;
// else, create new object
if (AssignObj.ObjectCounter >= max &&
PoolStore.Count > 0)
{
// Retrieve from pool
oAssignObj = RetrieveFromPool();
}
else
{
oAssignObj = GetNewAssignObj();
}
return oAssignObj;
}
private AssignObj GetNewAssignObj()
{
// Creates a new AssignObj
AssignObj oObj = new AssignObj();
PoolStore.Enqueue(oObj);
return oObj;
}
protected AssignObj RetrieveFromPool()
{
AssignObj oObj;
// Check if there are any objects in the collection
if (PoolStore.Count > 0)
{
oObj = (AssignObj)PoolStore.Dequeue();
AssignObj.ObjectCounter--;
}
else
{
// Return a new object
oObj = new AssignObj();
}
return oObj;
}
}
class AssignObj
{
public static int ObjectCounter = 0;
public AssignObj()
{
++ObjectCounter;
}
private string _Firstname;
private string _Lastname;
private int _RollNumber;
private string _Class;
public string Firstname
{
get
{
return _Firstname;
}
set
{
_Firstname = value;
}
}
public string Lastname
{
get
{
return _Lastname;
}
set
{
_Lastname = value;
}
}
public string Class
{
get
{
return _Class;
}
set
{
_Class = value;
}
}
public int RollNumber
{
get
{
return _RollNumber;
}
set
{
_RollNumber = value;
}
}
}
}

Output

Object

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published.