0

I need to store three diffrent types of object in a object pool and need to retrive depending on the need.

I tried implementing it with Queue,but i was able to do it with only one type.

Is it possible to store and retrive diffrent type of object in Object Pool?How?

NIlesh Lanke
  • 1,213
  • 9
  • 26
  • 35
  • You could have your different types have a common interface and use a Queue of this interface type. – Filburt Feb 08 '12 at 09:51
  • without further detaiils on your task it will be impossible to give detailed advice. Do your objects need a key in order to be retrieved? If you partly succeeded with a queue, I suppose all objects are identical and the order of retrieval from the pool does not matter? – user492238 Feb 08 '12 at 11:09
  • possible duplicate of [C# Object Pooling Pattern implementation](http://stackoverflow.com/questions/2510975/c-sharp-object-pooling-pattern-implementation) – Oliver Feb 08 '12 at 11:14

4 Answers4

2

Consider using a generic pool implementation. Use a distinct pool for every type of object needed to be pooled:

private static Stack<T1> poolT1 = new Stack<T1>(); 
...
T1 myobject = poolT1.Pop(); 

I personally would suggest to prefer Stack over Queue since it gives back recently used objects which potentially improves memory locality - and hence cause better cache performance.

user492238
  • 4,094
  • 1
  • 20
  • 26
2

Queue<T> is not a good idea for object pooling, because you often access the data using a key or something. Therefore ConcurrentDictionary<string, object> is for this case, in a multiple-thread application. You can wrap it to make your code clearer:

public class Pool
{
    private ConcurrentDictionary<string, object> m_data = new ConcurrentDictionary<string, object>();

    public T Take<T>(string key)
    {
        //fetch data from the dictionary and convert it to the type you want
    }

    //other methods like Insert...
}
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
1

There's a difference between System.Collections.Queue and System.Collections.Generic.Queue. I think you are referencing the latter. The former is weakly typed, the latter is strongly typed (i.e you have to specify the type).

The drawback of using objects is performance, because you have to box/unbox every time you get an object for inspection (i.e. you cast it) and you always have to have some kind of a bi switch statement to decide which type this object is. You can get around this by using additional data structures for storing typed information about the object.

Candide
  • 30,469
  • 8
  • 53
  • 60
0

You could have a Stack for each type of objects you want to pool. In your case, that would be 3 stacks. Then inside your Pool's Get method, you would have logic to determine which pool should be used to get the proper object. I do it by passing a parameter to the Get method, but it depends on your particular implementation.

public IType Get(int type)
{
    switch (type)
    {
        case 1:
            if (pool1.Count == 0)
            {
                return new MyObject1();
            }
            else
            {
                return pool1.Pop();
            }
            break;
        case 2:
            if (pool2.Count == 0)
            {
                return new MyObject2();
            }
            else
            {
                return pool2.Pop();
            }
            break;
        case 3:
            if (pool3.Count == 0)
            {
                return new MyObject3();
            }
            else
            {
                return pool3.Pop();
            }
            break;
     }
}

In your Free method, you can have an If...Else like this:

public void Free(IType obj)
{
    if (obj is MyObject1)
    {
        if (pool1.Count < MAX_POOL_SIZE)
            pool1.Push(obj);
    }
    else if (obj is MyObject2)
    {
        if (pool2.Count < MAX_POOL_SIZE)
            pool2.Push(obj);
    }
    else if (obj is MyObject2)
    {
        if (pool3.Count < MAX_POOL_SIZE)
            pool3.Push(obj);
    }
}

Ideally, you have control over the class of the objects being pooled. In which case, you would add a properties to them that would explicitly specify their true type, then in your Free method, you would do your logic on that property instead of having to use reflection. That would give you better performance.

Didier A.
  • 4,609
  • 2
  • 43
  • 45