Raj,
>>
For example, just before we
need a Connection object, can we call ConnectionPoolManager.getInstance()
and then call the getConnection() method ? That means for every servlet
which accesses the database, there will be a new instance of this Singleton
?
<<
This doesn't really mean you'll get a new instance for every servlet:
public class ConnectionPoolManager {
// private member to hold the one and only instance of the class
private ConnectionPoolManger thePool = null;
// private constructor
private ConnectionPoolManager( ... whatever you need ...);
// static to get hold of the one and only instance
static ConnectionPoolManager getInstance() {
// check if we have a pool
if(thePool == null) {
thePool = new ConnectionPoolManager(...);
}
return thePool;
}
}
So the first servlet that invokes getInstance() efectively causes the
singleton
to come into existance. From then on all calls to getInstance() simply serve
back a reference to the one and only instance of the object.
2. Whether you implement your ConfigurationManager as a singleton is really
a matter of choice. As it represents a single entity I would use a
singleton.
If all you want is to expose the values of your properties file in an object
then it doesn't really make a difference but instantiating multiple objects
that all represent the same entity doesn't feel right to me.
And what would happen if you wanted to change the values held in
the ConfigurationManager at runtime?
3. Again, as it represents the same entity a singleton would be my choice.
Make sure you synchronize your writeLog() method to avoid multiple log
clients
mixing their messages together.
You may want to consider leaving your LogWriter class as is, and creating a
a wrapper singleton class around it.
public class DebugLogger extends LogWriter {
// private member to hold the one and only instance of the class
private DebugLogger theLogger = null;
// private constructor
private DebugLogger( ... whatever you need ...)
{
super(...);
}
// static to get hold of the one and only instance
static DebugLogger getInstance() {
// check if we have a logger
if(theLogger == null) {
theLogger = new DebugLogger(...);
}
return theLogger;
}
}
hth
Graham
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html