<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>http://rasimsen.com/index.php?action=history&amp;feed=atom&amp;title=Object_Pool_Design_Pattern</id>
	<title>Object Pool Design Pattern - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://rasimsen.com/index.php?action=history&amp;feed=atom&amp;title=Object_Pool_Design_Pattern"/>
	<link rel="alternate" type="text/html" href="http://rasimsen.com/index.php?title=Object_Pool_Design_Pattern&amp;action=history"/>
	<updated>2026-04-16T08:12:09Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>http://rasimsen.com/index.php?title=Object_Pool_Design_Pattern&amp;diff=560&amp;oldid=prev</id>
		<title>Rasimsen: Created page with &quot;The idea of the Object Pool pattern is similar to that of a real-life book library. Every one knows that it is cheaper to go to you library and borrow a book than to buy a cop...&quot;</title>
		<link rel="alternate" type="text/html" href="http://rasimsen.com/index.php?title=Object_Pool_Design_Pattern&amp;diff=560&amp;oldid=prev"/>
		<updated>2018-08-10T17:47:27Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;The idea of the Object Pool pattern is similar to that of a real-life book library. Every one knows that it is cheaper to go to you library and borrow a book than to buy a cop...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;The idea of the Object Pool pattern is similar to that of a real-life book library. Every one knows that it is cheaper to go to you library and borrow a book than to buy a copy for yourself. Likewise, it is cheaper (in regards to system memory and speed) for a process to borrow an object rather than to instantiate it.&lt;br /&gt;
&lt;br /&gt;
The Object Pool lets others &amp;quot;check out&amp;quot; objects from its pool, when those objects are no longer needed by their processes, they are returned to the pool in order to be reused. However, we don&amp;#039;t want a process to have to wait for a particular object to be released, so the Object Pool also instantiates new objects as they are required, but must also implement a facility to clean up unused objects periodically.&lt;br /&gt;
&lt;br /&gt;
==Applicability / Uses==&lt;br /&gt;
Use the Object Pool pattern when:&lt;br /&gt;
&lt;br /&gt;
*your application sporadically requires objects which are &amp;quot;expensive&amp;quot; to create.&lt;br /&gt;
*several parts of your application require the same objects at different times.&lt;br /&gt;
&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Factory Method&amp;#039;&amp;#039;&amp;#039;: The Factory Method pattern can be used to encapsulate the creation logic for objects. However, it does not manage them after their creation, the object pool pattern keeps track of the objects it creates.&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Singleton&amp;#039;&amp;#039;&amp;#039;: Object Pools are usually implemented as Singletons.&lt;br /&gt;
&lt;br /&gt;
==Structure==&lt;br /&gt;
This Object Pool handles storage, tracking and expiration times. The instantiation, validation and destruction of specific object types must be handled by subclassing.&lt;br /&gt;
&lt;br /&gt;
UML diagram of the sample code:&lt;br /&gt;
&lt;br /&gt;
http://best-practice-software-engineering.ifs.tuwien.ac.at/patterns/images/objectpool.jpg&lt;br /&gt;
&lt;br /&gt;
==Sample==&lt;br /&gt;
Let us create our ObjectPool class. This code, while heavily modified to reflect new features in the Java language, is based on an example found on JavaWorld.com - Build your own ObjectPool in Java to boost app speed, by Thomas E. Davis, 06/01/98&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public abstract class ObjectPool&amp;lt;T&amp;gt; {&lt;br /&gt;
  private long expirationTime;&lt;br /&gt;
&lt;br /&gt;
  private Hashtable&amp;lt;T, Long&amp;gt; locked, unlocked;&lt;br /&gt;
&lt;br /&gt;
  public ObjectPool() {&lt;br /&gt;
    expirationTime = 30000; // 30 seconds&lt;br /&gt;
    locked = new Hashtable&amp;lt;T, Long&amp;gt;();&lt;br /&gt;
    unlocked = new Hashtable&amp;lt;T, Long&amp;gt;();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  protected abstract T create();&lt;br /&gt;
&lt;br /&gt;
  public abstract boolean validate(T o);&lt;br /&gt;
&lt;br /&gt;
  public abstract void expire(T o);&lt;br /&gt;
&lt;br /&gt;
  public synchronized T checkOut() {&lt;br /&gt;
    long now = System.currentTimeMillis();&lt;br /&gt;
    T t;&lt;br /&gt;
    if (unlocked.size() &amp;gt; 0) {&lt;br /&gt;
      Enumeration&amp;lt;T&amp;gt; e = unlocked.keys();&lt;br /&gt;
      while (e.hasMoreElements()) {&lt;br /&gt;
        t = e.nextElement();&lt;br /&gt;
        if ((now - unlocked.get(t)) &amp;gt; expirationTime) {&lt;br /&gt;
          // object has expired&lt;br /&gt;
          unlocked.remove(t);&lt;br /&gt;
          expire(t);&lt;br /&gt;
          t = null;&lt;br /&gt;
        } else {&lt;br /&gt;
          if (validate(t)) {&lt;br /&gt;
            unlocked.remove(t);&lt;br /&gt;
            locked.put(t, now);&lt;br /&gt;
            return (t);&lt;br /&gt;
          } else {&lt;br /&gt;
            // object failed validation&lt;br /&gt;
            unlocked.remove(t);&lt;br /&gt;
            expire(t);&lt;br /&gt;
            t = null;&lt;br /&gt;
          }&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    // no objects available, create a new one&lt;br /&gt;
    t = create();&lt;br /&gt;
    locked.put(t, now);&lt;br /&gt;
    return (t);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public synchronized void checkIn(T t) {&lt;br /&gt;
    locked.remove(t);&lt;br /&gt;
    unlocked.put(t, System.currentTimeMillis());&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The three remaining methods are abstract and therefore must be implemented by the subclass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class JDBCConnectionPool extends ObjectPool&amp;lt;Connection&amp;gt; {&lt;br /&gt;
&lt;br /&gt;
  private String dsn, usr, pwd;&lt;br /&gt;
&lt;br /&gt;
  public JDBCConnectionPool(String driver, String dsn, String usr, String pwd) {&lt;br /&gt;
    super();&lt;br /&gt;
    try {&lt;br /&gt;
      Class.forName(driver).newInstance();&lt;br /&gt;
    } catch (Exception e) {&lt;br /&gt;
      e.printStackTrace();&lt;br /&gt;
    }&lt;br /&gt;
    this.dsn = dsn;&lt;br /&gt;
    this.usr = usr;&lt;br /&gt;
    this.pwd = pwd;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  @Override&lt;br /&gt;
  protected Connection create() {&lt;br /&gt;
    try {&lt;br /&gt;
      return (DriverManager.getConnection(dsn, usr, pwd));&lt;br /&gt;
    } catch (SQLException e) {&lt;br /&gt;
      e.printStackTrace();&lt;br /&gt;
      return (null);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  @Override&lt;br /&gt;
  public void expire(Connection o) {&lt;br /&gt;
    try {&lt;br /&gt;
      ((Connection) o).close();&lt;br /&gt;
    } catch (SQLException e) {&lt;br /&gt;
      e.printStackTrace();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  @Override&lt;br /&gt;
  public boolean validate(Connection o) {&lt;br /&gt;
    try {&lt;br /&gt;
      return (!((Connection) o).isClosed());&lt;br /&gt;
    } catch (SQLException e) {&lt;br /&gt;
      e.printStackTrace();&lt;br /&gt;
      return (false);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
JDBCConnectionPool will allow the application to borrow and return database connections:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    // Do something...&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
    // Create the ConnectionPool:&lt;br /&gt;
    JDBCConnectionPool pool = new JDBCConnectionPool(&lt;br /&gt;
      &amp;quot;org.hsqldb.jdbcDriver&amp;quot;, &amp;quot;jdbc:hsqldb://localhost/mydb&amp;quot;,&lt;br /&gt;
      &amp;quot;sa&amp;quot;, &amp;quot;secret&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    // Get a connection:&lt;br /&gt;
    Connection con = pool.checkOut();&lt;br /&gt;
&lt;br /&gt;
    // Use the connection&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
    // Return the connection:&lt;br /&gt;
    pool.checkIn(con);&lt;br /&gt;
    &lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rasimsen</name></author>
		
	</entry>
</feed>