|
Hexagon
|
A generic, one-list object manager. This class maintains a single collection of objects and uses a delegate to determine whether each object is currently "active" or "inactive".
It is designed for scenarios where objects have a distinct active/inactive state that is managed externally, and the primary goal is to reuse inactive objects rather than creating new ones.
The pool's behavior is defined by four primary delegates provided during construction:
isActive will treat the newly gotten object as active.isActive will treat the released object as inactive.Note: isActive MUST be synchronized with onGet and onRelease. The state is NOT tracked internally.
Create a pool instance using the static Create method, providing the necessary delegates.
Example:
The primary and recommended way to retrieve an object is with the Get() method.
Get() Searches for the first available inactive object. If one is found, it is returned. If not, a new object is created using the factory, added to the pool, and then returned. onGet action is called automatically and the returned object is ready to use.
Release(T item) Releases the specified object and calls OnRelease action on it.
ReleaseAll() Releases all inactive objects.
Populate(int delta) Creates delta new objects and adds them to the pool. Their state is set by factory delegate.
Not recommended for general use. Must be used with caution.
ViewExistingInactive(out T result) Finds the first inactive object without changing its state. To finalize getting, Get(item) must be called. To discard getting no actions required.
ViewExistingActive(out T result) Finds the first active object without changing its state. To finalize releasing, Release(item) must be called. To discard releasing no actions required.
Get(T item) marks the specified object as gotten.
This pool does not internally manage the state of objects. It relies entirely on the isActive delegate to report the state. The user is responsible for ensuring that the onGet and onRelease actions correctly modify the object's state so that isActive returns the expected value.
All Get and View operations perform a linear search through the collection. Performance will degrade as the total number of objects in the pool increases. This implementation is best suited for small collections.