Re: Class.forName().newInstance() vs new
On 06/11/2011 09:43 PM, John B. Matthews wrote:
In article<it0brg$s3t$2@speranza.aioe.org>,
Abu Yahya<abu_yahya@invalid.com> wrote:
Could you give an example of how the factory method might look like?
There's some good examples here:
<http://en.wikipedia.org/wiki/Factory_method_pattern>
And in the linked article, "Item 1: Consider static factory methods
instead of constructors."
<http://drdobbs.com/java/208403883?pgno=1>
If Class.forName() is the bottleneck then a static factory method does
not help in this particular case. You would have to configure the
method name or a class name anyway and use reflection to get at the
instance or method.
Abu, to answer your question (pseudo code):
interface com.your.company.db.DbManager {
// methods
}
interface com.your.company.db.DbManagerFactory {
DbManager create(/* args if needed */) throws WhateverException
}
class com.your.company.db.ora.OraDbManager implements DbManager {
// ...
}
class com.your.company.db.ora.OracleDbManagerFactory implements
DbManagerFactory {
public DbManager create() {
return new OraDbManager();
}
}
Kind regards
robert