Object pool pattern: Difference between revisions

Content deleted Content added
m Description: Spelling/grammar/punctuation/typographical correction
See also: add portal
 
(7 intermediate revisions by 5 users not shown)
Line 1:
{{Short description|Software creational design pattern}}
{{for|the article about a general pool|Pool (computer science)}}
The '''object pool pattern''' is a software [[creational pattern|creational design pattern]] that uses a set of initialized [[Object (computer science)|objects]] kept ready to use – a "[[Pool (computer science)|pool]]" – rather than allocating and destroying them on demand. A client of the pool will request an object from the pool and perform operations on the returned object. When the client has finished, it returns the object to the pool rather than [[object destruction|destroying it]]; this can be done manually or automatically.
Line 35 ⟶ 36:
== Pitfalls ==
 
Care must be taken to ensure the state of the objects returned to the pool is reset back to a sensible state for the next use of the object, otherwise the object may be in ana state unexpected by the client, which may cause it to fail. The pool is responsible for resetting the objects, not the clients. Object pools full of objects with dangerously stale state are sometimes called object cesspools and regarded as an [[anti-pattern]].
 
Stale state may not always be an issue; it becomes dangerous when it causes the object to behave unexpectedly. For example, an object representing authentication details may fail if the "successfully authenticated" flag is not reset before it is reused, since it indicates that a user is authenticated (possibly as someone else) when they are not. However, failing to reset a value used only for debugging, such as the identity of the last authentication server used, may pose no issues.
Line 171 ⟶ 172:
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 class PooledObject
{
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 class PooledObject
{
private DateTime _createdAt = DateTime.Now;
public DateTime CreatedAt
{
get { return _createdAt; }
}
public string TempData { get; set; }
}
 
public DateTime CreatedAt => _createdAt;
// 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
public string TempData { get; set; }
// are returned to a suitable state, ready for reuse.
}
public static class Pool
 
// 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
{
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;
}
}
Line 328 ⟶ 325:
 
== See also ==
{{Portal|Computer programming}}
* [[Connection pool]]
* [[Free list]]
Line 363 ⟶ 361:
[[Category:Software optimization]]
[[Category:Articles with example C Sharp code]]
[[Category:Articles with example Java code]]