I am asked to answer the below question as part of a assessment in a hiring process. I have no problem with object return, hence if one could point me in the right direction with class that supports any number of object types I'll be a happy fella.
You are to develop a class that
can be used to pool memory by instantiating objects outside of time critical
areas and allow consumers to "borrow" and "return" the objects.
There should be one implementation of this new class that will support
any number of object types for pooling.
Loading
VulpesPosted Jun 13, 2013, 8:51 AM
I think what's needed is a generic class, Pool
As there's no point in having more than one pool for any T, I'd make the class into a singleton with a static GetInstance method to either create the pool or return the existing instance plus, of course, a private static field to store the instance and a private instance constructor to create it. The GetInstance method could take a parameter indicating how many objects are to be created initially within the pool
As the class will need to instantiate objects of type T, you'll also need to add the new() constraint which will restrict T to those classes which have a parameterless constructor.
Internally, you could use a Queue
When an object is returned, this can then be enqueued for subsequent reuse.