What is object pool?
As a developer we might have noticed that creation of some class is very difficult and number of time that we need to create. Such object creation every time is very expensive in terms of system resources. If we can cache that kind of object show how will surely boost the application performance and resource usage can be saved. This where object pool design pattern will help development community to cache the objects. Some time it is also called as Object cache or Resource cache design pattern.
It is adviced to keep all Reusable expensive objects that are not currently in use in the container so that they can be managed by one rational policy. To achieve this, the Reusable Pool class is designed to be a singleton class.
Queue Class
Queue is a Simple DataStucture which allows Add or Remove of Items at one of the ends only. It is basically called as First in First out data structure. The item which is added first is the first one to be removed. Default initial size of the queue is 32. Make sure that whenever Queue is initialized define the size of the queue, that Add and Remove operations are performed perfectly.
Enqueue () method will add the item in the last available portion and Dequeue () method will remove and return the oldest item in the queue. Set us see the code how it helps to implement the object pool design pattern.
EX: Simple Example
- public class QueuedPool: Queue
- {
- public Person Fetch()
- {
- return (Person)Dequeue();
- }
- public void Store(Person person)
- {
- Enqueue(person);
- }
- public void ClearAll()
- {
- base.Clear();
- }
- }
- QueuedPool queuePool = new QueuedPool();
- queuePool.Store(new Person());
- Person p = queuePool.Fetch();
- public interface IStore<T>
- {
- T Fetch();
- void Store(T obj);
- int Count { get;}
- void ClearAll();
- }
- public class QueuedPool<T> : Queue<T>, IStore<T>
- {
- public T Fetch()
- {
- return (T)Dequeue();
- }
- public void Store(T obj)
- {
- Enqueue(obj);
- }
- public void ClearAll()
- {
- base.Clear();
- }
- }
- QueuedPool<Person> queuePool = new QueuedPool<Person>();
- queuePool.Store(new Person());
- Person p = queuePool.Fetch();
- public sealed class ObjectPoolObject<T> where T : new()
- {
- private readonly int _poolSize;
- private readonly object _threadLocker;
- private readonly Queue<T> _poolManager;
- public ObjectPoolObject(int expectedPoolSize)
- {
- if (expectedPoolSize <= 0)
- throw new ArgumentOutOfRangeException("expectedPoolSize", "Pool size can't be null or zero or less than zero");
- _poolManager = new Queue<T>();
- _threadLocker = new object();
- _poolSize = expectedPoolSize;
- }
- public T Get()
- {
- lock (_threadLocker)
- {
- if (_poolManager.Count > 0)
- {
- return _poolManager.Dequeue();
- }
- else
- {
- return new T();
- }
- }
- }
- public int Count()
- {
- return _poolManager.Count;
- }
- public void Put(T obj)
- {
- lock(_threadLocker)
- {
- if (_poolManager.Count < _poolSize)
- _poolManager.Enqueue(obj);
- }
- }
- }

Alex DavilaPosted May 28, 2012, 6:29 PM
My mail is [email protected], i need the source code with C# project
Alex DavilaPosted May 28, 2012, 6:28 PM
I need the C# project please...