Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: Class.forName().newInstance() vs new Date: Sun, 12 Jun 2011 14:14:10 +0200 Lines: 45 Message-ID: <95josjFf62U1@mid.individual.net> References: <95ho4qFd7cU1@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net Y1815lEKs3XLMkkRyrsrmgR5+Yob4vY/D9fdQlmq5JspBi+hQ= Cancel-Lock: sha1:yxNXAVW/yBm7P3sChV84KKGApcU= User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110516 Lightning/1.0b2 Thunderbird/3.1.10 In-Reply-To: Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5234 On 06/11/2011 09:43 PM, John B. Matthews wrote: > In article, > Abu Yahya wrote: > >> Could you give an example of how the factory method might look like? > > There's some good examples here: > > > > And in the linked article, "Item 1: Consider static factory methods > instead of constructors." > > 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