Object pool pattern: Difference between revisions

Content deleted Content added
m Java: Fix indentation
See also: add portal
 
(8 intermediate revisions by 6 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 5 ⟶ 6:
 
== Description ==
When it is necessary to work with a large number ofnumerous objects that are particularly expensive to instantiate and each object is only needed for a short period of time, the performance of an entire application may be adversely affected. An object pool design pattern may be deemed desirable in cases such as these.
 
The object pool design pattern creates a set of objects that may be reused. When a new object is needed, it is requested from the pool. If a previously prepared object is available, it is returned immediately, avoiding the instantiation cost. If no objects are present in the pool, a new item is created and returned. When the object has been used and is no longer needed, it is returned to the pool, allowing it to be used again in the future without repeating the computationally expensive instantiation process. It is important to note that once an object has been used and returned, existing references will become invalid.
 
In some object pools the resources are limited, so a maximum number of objects is specified. If this number is reached and a new item is requested, an exception may be thrown, or the thread will be blocked until an object is released back into the pool.
 
The object pool design pattern is used in several places in the standard classes of the .NET Framework. One example is the .NET Framework Data Provider for SQL Server. As SQL Server database connections can be slow to create, a pool of connections is maintained. Closing a connection does not actually relinquish the link to SQL Server. Instead, the connection is held in a pool, from which it can be retrieved when requesting a new connection. This substantially increases the speed of making connections.
 
== Benefits ==
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]]