H.Object Pool
Summary
Reuse and share objects that are expensive to create.
Whenever there are several clients who needs the same stateless resource which is expensive to create.
Pools are generally singleton and created reusable object like database connection or HTTP Connection.
When to Use
When resources are limited and you want to use in controlled manner
Overview Tutorial
Class Diagram
Client holds a class called Pool that contains object of client interest.
Objects are marked as Acquired or Releases.
Once Client request an object pool return any available object marking them as Acquired
If no resources available then pool wait for object availability.
In order to wait improve performance pool must recycled the object that are not in use, this can be implemented by using timeout with resource.
Example
Database connection sharing or thread pools are good example of it.
Network Connection Manager
Specific Problems and Consideration
Handling unused resources
When the Object Pool pattern is used the objects should be marked as available(released) by the client after they are used, so the pool will be aware about this.
This is the main drawback because the client should do this and it's a common situation when database connection is not released after they are used.
To overcome this problem cleanup mechanism can be implemented to release resources if they are not used for a period of time.
Handling Resources Creation Failure
Creating the resources might fail and this case should be treated carefully.
When there is no available resource (because the number is limited or creating a new one failed) the client should be notified about it.
Synchronization Issue
Although the object pool is handling the object instantiation it`s main purpose is to provide a way for the clients to reuse the objects like they are new objects, without being shared and reused.
This present synchronization as challenge because object access need to be mutually exclusive.