Object pool pattern: Difference between revisions

Content deleted Content added
Altered wording, changed from second person to third person.
Reversed the main premise of this passage. Automated object pools are possible in C++ via smart pointers; Only manual possible in Java/C# as finalizers are really a bad idea.
Line 34:
 
== Implementation ==
Object pools can be implemented in an automated fashion in languages like C++ via smart pointers. In the constructor of the smart pointer - an object can be requested from the pool and in the destructor of the smart pointer - the object can be released back to the pool. In garbage collected languages, where there are no destructors (which are guaranteed to be called as part of a stack unwind) - object pools MUST be implemented in a manual fashion, by explicitly requesting an object from [[Factory (object-oriented programming)|factory]] and returning the object via calling a dispose method, as in the [[dispose pattern]]. Using a [[finalizer]] is not a good idea as there are usually no guarantees on when (or if ever) the finalizer will be run.
Object pools can be implemented manually, by explicitly requesting an object from a [[Factory (object-oriented programming)|factory]] and returning the object via calling a dispose method, as in the [[dispose pattern]]. Alternatively, in garbage-collected languages, object pools can be implemented automatically for a [[Class (computer programming)|class]] by having the [[Constructor (object-oriented programming)|constructor]] return an object from the pool, and the [[finalizer]] return an object to the pool, using [[object resurrection]] to return the object to life.{{sfn|Goldshtein|Zurbalev|Flatow|2012|p=[http://books.google.co.jp/books?id=D3J58cs-i44C&pg=PA129&q=resurrection#v=onepage&q=resurrection 129]}} Note that automatic object pools are not possible if using a [[Destructor (computer programming)|destructor]], as this guarantees destruction of the object.
 
Manual object pools are simple to implement, but harder to use, as they require [[manual memory management]] of pool objects. By contrast, automatic object pools are easy to use – indeed, transparent – but require language support for multiple finalization and significantly complicate [[garbage collection (computer science)|garbage collection]]; automatic object pools are one of the few standard applications of object resurrection.{{sfn|Goldshtein|Zurbalev|Flatow|2012|p=[http://books.google.co.jp/books?id=D3J58cs-i44C&pg=PA129&q=resurrection#v=onepage&q=resurrection 129]}}
 
== Handling of empty pools ==