0
Widget oPooledWidget = (Widget)oObjectPool.borrowObject();
Widget oInjectedWidget = oAppContext.getBeans("widget");
Widget oFactoryWidget = oWidgetFactory.createWidget();

Many different ways exist to instantiate classes without the new operator and a constructor, and I'm looking for the generally-accepted use cases/scenarios for when to choose one over the other.

Obviously it is not appropriate to pool objects in certain circumstances, such as when you either have no idea how big the pool could get, or when you know the pool would be huge.

But what is an appropriate scenario that calls for pooling? When does pooling offer benefits over dependency injection or factory classes?

Similarly, when would one want to choose using a factory class over dependency injection, and vice versa?

skaffman
405k96 gold badges825 silver badges775 bronze badges
asked Sep 21, 2011 at 13:10

1 Answer 1

2

Dependency injection came from a time when code had gazillion of Factories. So, DI is a convinient replacement for Factories. You can call DI context as a universal Factory. Also, object pool can be a bean defined in DI context. Though object pool implements an interface that application code must use in order to keep the pool consistent. Objects retrieved from the pool have different lifecycle compared to DI context beans:

pool = appContext.getBean("connectionPool");
conn = pool.get();
try {
 // .. do stuff
} finally {
 conn.close();
 // or
 pool.release(conn);
}
answered Sep 21, 2011 at 13:22
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.