Name

Resource Pool

Goal

Decrease costs of instantiating and maintaining large objects using a pool of pre-generated objects (Figure A-15).

Classes in the Resource Pool pattern
Figure A-15. Classes in the Resource Pool pattern

Participants

Pool

A limited collection of pre-generated objects that can be loaned out and returned.

Factory

Used by the pool to generate new instances of a resource or validate a returned resource.

Resource

An object that will be loaned out by the pool (Figure A-16).

Interactions in the Resource Pool pattern
Figure A-16. Interactions in the Resource Pool pattern

Interactions

The client requests objects from the pool, which returns a resource if one is available. If no resources are available, but the pool is not at capacity, a new instance is generated using the factory. If all resources are loaned out, the client waits until one becomes available. When the client has finished using the resource, it returns it to the pool, which uses the factory to validate the returned resource.

Notes

A resource pool can easily be implemented based on the Java collections APIs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset