.NET ile bir Object Pool yazdım. Özetle ilk oluşturulurken fazla memory/CPU gerektiren Class' ları cachlemek için kullanılıyor. Biraz internette aradım .NET için hazır kod bulamadım.
Genelde Object Pool implementasyonu objelerin belli bir zamandan sonra silinmesini de destekler, onu henüz bu class desteklemiyor. Bu Generic bir class kullanabilmeniz için kendi class' ınızı bundan inherit etmeniz gerekli. Genelde Object Pool' u Singeleton olarak tanımlamak isteyebilirsiniz, ben bunu yapmadım çünkü gereksinimler farklı olabilir. Birden fazla pool ihtiyacınız olabilir, bu da gene kendi class' ınıza kalmış bir şey, genelde o class' ı singleton yapmanız işleri kolaylaştıracaktır.
Örnek projeyi VS.NET 2008 proje dosyası olarak download edebilirsiniz.
Aşağıdaki Pooler ana class,
''' <summary>''' Pooler, Performance-wise Object Pool implementation.''' </summary>''' <remarks>''' Inherit your class, override Create(). Close() can be overridable if your object
''' requires special clear-up override it and it will be called for all pooled objects when Clear() called.
''' </remarks>''' <developer>''' Ferruh Mavituna (http://ferruh.mavituna.com)''' 10/02/2008''' </developer>Public MustInherit Class Pooler(Of T)
#Region "Constructor"
''' <summary> ''' Initializes a new instance of the <see cref="Pooler" /> class. ''' </summary>Public Sub New()
End Sub
#End Region#Region "Lists"
Private Const STARTUP_LIST As Integer = 10
''' <summary> ''' Lock Object for lists ''' </summary>Private ListLock As New Object()
''' <summary> ''' Available Objects in the Pool ''' </summary>Private AvailableObjects As New List(Of T)(STARTUP_LIST)
''' <summary> ''' Currently Active, Unavailable Objects ''' </summary> ''' <remarks></remarks>Private ActiveObjects As New List(Of T)(STARTUP_LIST)
#End Region#Region "Overrides"
''' <summary> ''' Creates this instance. ''' </summary> ''' <returns></returns>MustOverride Function Create() As T
''' <summary> ''' Clears the specified obj. ''' </summary> ''' <param name="Obj">The obj.</param>Public Overridable Sub Close(ByVal Obj As T)
Obj = NothingEnd Sub
#End Region#Region "Core Action"
''' <summary> ''' Get a new T from pool. ''' </summary> ''' <returns></returns>Public Function Acquire() As T
Dim CurrentT As T
'If there is no available object creat a new oneIf AvailableObjects.Count = 0 Then
CurrentT = Me.Create() Else 'If there is an object already available get it SyncLock ListLock 'Get and remove from Available object CurrentT = AvailableObjects(0)
AvailableObjects.Remove(CurrentT)
End SyncLock
End If
'Add active object to ActiveObjects SyncLock ListLockActiveObjects.Add(CurrentT)
End SyncLock
Return CurrentTEnd Function
''' <summary> ''' Releases the specified object. ''' </summary> ''' <param name="releaseObject">The object to release.</param>Public Sub Release(ByVal releaseObject As T)
SyncLock ListLockActiveObjects.Remove(releaseObject)
AvailableObjects.Add(releaseObject)
End SyncLock
End Sub
''' <summary> ''' Clears the pool. ''' </summary>Public Sub Clear()
Clear(ActiveObjects)
Clear(AvailableObjects)
End Sub
''' <summary> ''' Clears the specified list. ''' </summary> ''' <param name="list">The list.</param>Private Sub Clear(ByVal list As List(Of T))
For Each Obj As T In list
Close(Obj)
Nextlist.Clear()
End Sub
#End RegionEnd Class
Bunu Kullanan Örnek Bir Class,
1: Public Class Expensive
2: 3: Public CreateTime As DateTime
4: 5: Public Event Initiliased(ByVal sender As Object)
6: 7: Public Sub New()
8: Console.Write(".")
9: 10: 'Really Expensive stuff..!
11: Threading.Thread.Sleep(300) 12: CreateTime = Now13: End Sub
14: 15: Public Sub WriteAndFinish()
16: Console.WriteLine(Me.CreateTime.ToShortTimeString)
17: RaiseEvent Initiliased(Me)
18: End Sub
19: 20: End Class