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
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;
public DateTime CreatedAt▼
{▼
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▼
▲
▲
{
{
▲ 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;
}
}
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]]
|