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
{
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
{
{▼
get { return _createdAt; }▼
}▼
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. ▼
▲
public static class Pool▼
▲
{
public static PooledObject GetObject()
{
▲ private static List<PooledObject> _inUse = new List<PooledObject>();
▲ public static PooledObject GetObject()
{
{
return
_available.RemoveAt(0);▼
return po;▼
▲ PooledObject po = new PooledObject();
}
public static void ReleaseObject(PooledObject po)▼
{▼
CleanUp(po);▼
{
_inUse.
}
}
private static void CleanUp(PooledObject po)▼
{
}
}
po.TempData = null;
}
}
|