Jump to content
  • Advertisement
Sign in to follow this  
haora

starting patterns, ObjectPool

This topic is 4910 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, so, after asking here..., and looking on the net, I found some interesting stuff... Now I'm reading the book "Thinking in Patterns", I found a digital versión of it from the author..., the thing is, when he shows the code for the "ObjectPool", I understand the implementation of that pattern, but then, it uses a "Singleton" to manage some how, the ObjectPool...., I even understand the concept, but the problem is, that when he implements the singleton, he doesn't declare the constructor for that clase (which, for what I understand, should be private)..., wouldn't that be a problem, since the compiler would assign a general one??? The code for the singleton is here:
class ConnectionPool { // A singleton
  private static PoolManager pool = new PoolManager();
  public static void addConnections(int number) {
    for(int i = 0; i < number; i++)
      pool.add(new ConnectionImplementation());
  }

  public static Connection getConnection()
  throws PoolManager.EmptyPoolException {
    return (Connection)pool.get();
  }

  public static void releaseConnection(Connection c) {
    pool.release(c);
  }
}

And then, he uses it like this:
public class ConnectionPoolDemo extends TestCase  {
  static {
    ConnectionPool.addConnections(5);
  }
  public void test(){
    Connection c = null;
    try {
      c = ConnectionPool.getConnection();
    } catch (PoolManager.EmptyPoolException e) {
      throw new RuntimeException(e);
    }
    c.set(new Object());
    c.get();
    ConnectionPool.releaseConnection(c);
  }
  public void test2() {
    Connection c = null;
    try {
      c = ConnectionPool.getConnection();
    } catch (PoolManager.EmptyPoolException e) {
      throw new RuntimeException(e);
    }
    c.set(new Object());
    c.get();
    ConnectionPool.releaseConnection(c);
  }
  public static void main(String args[]) {
    junit.textui.TestRunner.run(ConnectionPoolDemo.class);
  }
} ///:~

Thanks in advance....

Share this post


Link to post
Share on other sites
Advertisement
I definitely think you are right about the private constructor and I'd recommend that. But in this case it does not make much difference. Since all member functions are static, an instance created via the default constructor would not be of any use other than what was already available via the static interface. So: wrong no. Ugly: yes.

You realizing this is a good sign you are picking up, in my opinion!

Greetz,

Illco

Share this post


Link to post
Share on other sites
My view is that if it allows you to create more than one instance then it really isn't a singleton but either a static namespace or something weird like that. In a large codebase you have to be prevented from making multiple instances and passing them to functions because that creates havoc.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!