Object pool pattern: Difference between revisions

Content deleted Content added
m Description: Spelling/grammar/punctuation/typographical correction
C#: use file-scoped namespace
Line 171:
The following shows the basic code of the object pool design pattern implemented using C#. For brevity the properties of the classes are declared using C# 3.0 automatically implemented property syntax. These could be replaced with full property definitions for earlier versions of the language. Pool is shown as a static class, as it's unusual for multiple pools to be required. However, it's equally acceptable to use instance classes for object pools.
<syntaxhighlight lang="csharp">
namespace DesignPattern.Objectpool ;
 
// The PooledObject class is the type that is expensive or slow to instantiate,
// or that has limited availability, so is to be held in the object pool.
public staticclass PooledObject GetObject()
{
private DateTime _createdAt = DateTime.Now;
// The PooledObject class is the type that is expensive or slow to instantiate,
 
// or that has limited availability, so is to be held in the object pool.
public classDateTime PooledObjectCreatedAt
{
privateget DateTime{ _createdAtreturn = DateTime.Now_createdAt; }
public DateTime CreatedAt
{
get { return _createdAt; }
}
public string TempData { get; set; }
}
 
public string TempData { get; set; }
// The Pool class controls access to the pooled objects. It maintains a list of available objects and a
}
// collection of objects that have been obtained from the pool and are in use. The pool ensures that released objects
 
// are returned to a suitable state, ready for reuse.
// The Pool class controls access to the pooled objects. It maintains a list of available objects and a
public static class Pool
// collection of objects that have been obtained from the pool and are in use. The pool ensures that released objects
// are returned to a suitable state, ready for reuse.
public static class Pool
{
private static List<PooledObject> _inUse_available = new List<PooledObject>();
private static List<PooledObject> po_inUse = new List<PooledObject>();
 
public static PooledObject GetObject()
{
private static List<PooledObject>lock (_available = new List<PooledObject>();
private static List<PooledObject> _inUse = new List<PooledObject>();
public static PooledObject GetObject()
{
lockif (_available.Count != 0)
{
ifPooledObject (_available.Countpo != _available[0)];
{_inUse.Add(po);
PooledObject po = _available[.RemoveAt(0]);
return _inUse.Add(po);
_available.RemoveAt(0);
return po;
}
else
{
PooledObject po = new PooledObject();
_inUse.Add(po);
return po;
}
}
} else
public static void ReleaseObject(PooledObject po)
{
CleanUp(po);
lock (_available)
{
_available.Add(PooledObject po = new PooledObject();
_inUse.RemoveAdd(po);
get { return _createdAtpo; }
}
}
}
 
private static void CleanUp(PooledObject po)
public static void ReleaseObject(PooledObject po)
{
CleanUp(po);
 
lock (_available.RemoveAt(0);
{
po_available.TempData = nullAdd(po);
return _inUse.Remove(po);
}
}
 
private static void CleanUp(PooledObject po)
{
po.TempData = null;
}
}