Unfortunately, StudioStyles doesn’t support all the styles for VB.Net, so you’ll have to tweak it a bit if you want VB.Net goodness.
This post has moved to Yagni.net.
Performance can be an important consideration for applications. In some scenarios, object creation is a costly step.
Avoids cost of initializing objects by maintaining a pool of pre-initialized objects that can be re-used.
Description of Benefits
Can offer a performance boost where:
- object instantiation is expensive
- instances of the class are frequently created
- number of instances at any one time is small
Can make initialization time predictable where it would otherwise be unpredictable (e.g. when squiring resources over a network)
Instantiation of objects that represent:
- database connections
- socket connections
- large graphic objects
- Shoe shelf at a bowling club
- Car pooling
Reusable: An object used by Client until they it is no longer required.
Client: Uses an instance of Reusable for a limited amount of time
ReusablePool: Manages the collection of Reusable objects by:
- creating new instances of Reusable
- supplying instances of Reusable to Clients
Typical methods induce:
- getInstance: A static method that returns an instance of ReusablePool
- aquireReusable:A method that returns an instance of Reusable
- releaseReusable(Reusable):A method that returns a Reusable to the pool
Basic Implementation (Collaboration)
The Client is responsible for requesting the Reusable from the ReusablePool.
On receiving a request for an object, the ReusablePool will attempt to supply an suitable Reusable.
The Client should be unaware that the Reusable can be shared with other Clients: from the Client‘s point of view, the Reusable can be treated in exactly the same way as an object that has been created in any other way,the only difference being that it must return it to the ReusablePool once it has finished using it.
The Client is responsible for returning Reusables to the ReusablePool once it is finished with.
Resource Loading Strategy
Several strategies are available, for example:
- Eager: A specified number of Reusables are created by the ReusablePool when the ReusablePool is instantiated.
- Lazy: Reusables are not created by the ReusablePool until they are requested by the a Client. Once Reusables are released, they are immediately available for other Clients.
- Hybrid: A specified number of Reusables are created eagerly, but additional Reusables are created lazily.
- Lazy-Expanding: Creates resources lazily, but doesn’t re-use them until the ReusablePool reaches a certain size.
- Eager-Expanding: Creates resources eagerly. Creates additional resources when available objects in the ReusablePool drops below a certain threshold.
Maximum Pool Size
Some implementations may set a maximum number of Reusables in the ReusablePool
Various strategies can be adopted to handle the situation where a Client requests an instance from the pool, but none is currently available:
Prevent the situation by ensuring that the ReusablePool will always contain enough Reusables.
- Fail to provide Reusable, and inform the Client that none is available
- Create a new Reusable, thus increasing the size of the pool
- Block until another thread to releases an object back into the pool
- Forcible reclaim a Reusable from a low-priority Client
In a multi-threaded environment, careful consideration must be given to synchronisation of methods on the ResourcePool object.
If the Client fails to return a Reusable to the pool, then it will be unavailable to other Clients. To avoid this, the ResourcePool could implement an expiry time for Reusable:
if the Reusable has not actually been used for a certain length of time, it can be made available to other Clients.
In some situations, it may be undesirable to hold unused Reusables in the ReusablePool for long periods of time. In this case, it may be desirable to evict Reusables from the ReusablePool if they have not been requested by a Client for a specified length of time.
Some types of Reusables may need to be reset to a know state before they can be allocated to another Client. This is the responsibility of the ReusablePool.
- Mixed Pools
Relationship with Other Patterns
ReusablePool is often implemented as a Singleton.
If you receive a “Service Unavailable” screen after publishing an ASP.Net web site, check the application pool assigned to the site. The pool you use needs to be assigned the right version of the .Net framework. For example, a .Net 2.0 site won’t work if the pool is using .Net 1.0.
In .Net it seems reasonable to try to pass a NULL to a SQL Server stored procedure using the following syntax (C#):
Unfortunately, if the sproc is expecting a parameter of type Varbinary(max), the following exception will be thrown:
Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.
This appears to be a bug in the .Net SqlDataAdapter.
The work-around is to be explicit about the type of data you are passing:
myCommand.Parameters.Add("@MyParameter", SqlDbType.VarBinary, -1); myCommand.Parameters["@MyParameter"].Value = DBNull.Value;
My thanks go to dnagelhout and Matt Neerincx for their invaluable posts on this subject.
Air Lift 70460
Pole Position 56600
Space Eagles 11880
Starship Command 889
Chucky Egg 235330
Snake (fast) 1016
Mr EE 103100
Some quick notes:
|The Desire for Possessions||The Desire for Physical Pleasure||The Desire for Power and Prestiege
|Genesis 3||Pleasant to the eyes||Good for food||Desire to be wise|
|1 Jn 2:15-17||Lust of the eyes||Lust of the flesh||Pride of life|
|Matthew 13||Riches||Pleasures||Cares (using own power)|
|Matthew 4:13||Kingdoms of the World||Stones to Bread||Throw yourself down (you’re so important, God won’t let youu hurt yourself)|
|Ezekiel 16:49||fulness of bread||abundance of idleness was in her and in her daughters … neither did she strengthen the hand of the poor and needy||pride|
Further Reading: Warfare on Three Fronts