Is it possible to create a generic object pool that creates new objects inside it? Plus it would be nice if this object creation could receive parameters.
public interface IPoolable
{
void Dispose();
}
public class ObjectPool<T> where T : IPoolable
{
private List<T> pool;
public T Get()
{
if(pool.count > 0)
{
return pool.Pop();
}
else
{
return new T(); // <- How to do this properly?
}
}
}
public class SomeClass : IPoolable
{
int id;
public SomeClass(int id)
{
this.id = id;
}
public void Dispose()
{
}
}
public class OtherClass : IPoolable
{
string name;
int id;
public OtherClass(string name, int id)
{
this.name = name;
this.id = id;
}
public void Dispose()
{
}
}
In a way that it can be used like this if it could receive parameters.
SomeClass a = myPool.Get(2);
OtherClass b = myOtherPool.Get("foo", 4);
Or this would also be fine if parameters were not possible.
SomeClass a = myPool.Get();
a.id = 2;
OtherClass b = myOtherPool.Get();
b.name = "foo";
b.id = 4;