Refactoring

What approach is better and effective:

publicstatic ClientFramework getInstance()

{

if (instanceObj ==null)

{

instanceObj =new ClientFramework();

}

return instanceObj;

}

publicstatic ClientFramework getInstance(Log logger)

{

if(instanceObj ==null)

{

instanceObj =new ClientFramework();

}

instanceObj.setLogger(logger);

return instanceObj;

}

or

publicstatic ClientFramework getInstance()

{

instanceCheckMethod();

return instanceObj;

}

publicstatic ClientFramework getInstance(Log logger)

{

instanceCheckMethod();

instanceObj.setLogger(logger);

return instanceObj;

}

privatevoid instanceCheckMethod(){

if (instanceObj ==null)

{

instanceObj =new ClientFramework();

}

}

[2074 byte] By [dbes@isd.dp.uaa] at [2007-11-26 13:10:37]
# 1

public static ClientFramework getInstance()

{

return getInstanceObj(DEFAULT_LOGGER);

}

public static ClientFramework getInstance(Log logger)

{

createClientFrameworkIfNecessary();

clientFramework.setLogger(logger);

return clientFramework;

}

private void instanceCheckMethod() {

if (clientFramework == null)

{

clientFramework = new ClientFramework();

}

}

~

yawmarka at 2007-7-7 17:25:03 > top of Java-index,Java Essentials,Java Programming...
# 2

Better for what? Effective? Both are, I guess. the second is certainly quite confusing, the first is just a non-threadsafe way to provide a singleton that isn't really one - the next instance that calls getInstance with a logger will replace the one originally set. Douesn't sound nice to me.

CeciNEstPasUnProgrammeura at 2007-7-7 17:25:03 > top of Java-index,Java Essentials,Java Programming...
# 3
well, what benefits do you see with the second example?in both instances, you have some repeated code: the check for null, followed by creation. factor that repetition out by having the getInstance(Log logger) method get it's ClientFramework from the first method.
georgemca at 2007-7-7 17:25:03 > top of Java-index,Java Essentials,Java Programming...
# 4
> Douesn't sound nice to me.Agreed.~
yawmarka at 2007-7-7 17:25:03 > top of Java-index,Java Essentials,Java Programming...
# 5

public static ClientFramework getInstance()

{

return getInstance(new Log());

}

public static synchronized ClientFramework getInstance(Log logger)

{

if(instanceObj == null){

instanceObj = new ClientFramework();

instanceObj.setLogger(logger);

}

return instanceObj;

}

Would be my suggestion. Depending on what's supposed to happen with the logger.

CeciNEstPasUnProgrammeura at 2007-7-7 17:25:03 > top of Java-index,Java Essentials,Java Programming...