I’m looking for a design pattern to manage a pool of objects. A thread can request an object, in my case an object representing a remote host.
If the host-object does not exist it is created, added to the pool and a reference returned.
If another thread or object requests the same (remote host) object it too is given the reference. If a different remote host is requested it is created and added to the ‘pool’ of objects.
Once the host-object is no longer needed by any thread or object is removes it self form the pool.
- Is there pattern for this, and what is it name?
- I will be implementing this in Objective-C, any specific help would be useful.
An alternative to using NSMapTable (which is unavailable on the iPhone, for instance) is to use the CoreFoundation equivalents. Those can contain regular non-retained pointers. You still need to pull things out of that list however, but that can be done easily in an object’s -dealloc method.
One possibility is using a CFSet. This would require that your objects all respond to
-isEqual:and-hashproperly, so that the set can determine if an object is already present accurately.For instance (note that I’ve not compiled this but it ought to be at least 90% correct):
Hopefully I’ve put enough in here to answer any questions/thoughts you might have about this design pattern.