Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualBubsy

Posted 10 November 2012 - 09:48 AM

I really don't like singletons, but there's a pattern I developed for a large scale project to overcome the lack of testabillty and the implementation coupling that the singletons usage brings :

[source lang="java"]public abstract class Settings { private static final SingletonHolder <Settings> HOLDER = new SingletonHolder<Settings>("implementation.property"); public static final Settings getInstance() { return HOLDER.getInstance(); } public abstract void foo();}public class DefaultSettings extends Settings { public void foo() { // Do your thing }}public class SingletonHolder <Type> { private final <Type> instance; public SingletonHolder(String property) { this.instance = Class.forName(System.getProperty(property)).newInstance(); } public Type getInstance() { return instance; }}[/source]

For the sake of brevity, I didn't write the exception handling code, and the SingletonHolder should be aware of changings on the value of the system property used to fetch the implementation's class name.

#2Bubsy

Posted 10 November 2012 - 09:47 AM

I really don't like singletons, but there's a pattern I developed for a large scale project to overcome the lack of testabillty and the implementation coupling that the singletons usage brings :

[source lang="java"]public abstract class Settings { private static final SingletonHolder <Settings> HOLDER = new SingletonHolder<Settings>("implementation.property"); public static final Settings getInstance() { return HOLDER.getInstance(); } public abstract void foo();}public class DefaultSettings extends Settings { public void foo() { // Do your thing }}public class SingletonHolder <Type> { private final <Type> instance; public SingletonHolder(String property) { this.instance = Class.forName(System.getProperty(property)).newInstance(); } public Type getInstance() { return instance; }}[/source]

For the sake of brevity, I didn't write the exception handling code, and the SingletonHolder should be aware of changings on the value of the system property used to fetch the implementation's class name.

#1Bubsy

Posted 10 November 2012 - 09:47 AM

I really don't like singletons, but there's a pattern I developed for a large scale project to overcome the lack of testabillty and the implementation coupling that the singletons usage brings :

[source lang="java"] public abstract class Settings { private static final SingletonHolder <Settings> HOLDER = new SingletonHolder<Settings>("implementation.property"); public static final Settings getInstance() { return HOLDER.getInstance(); } public abstract void foo();}public class DefaultSettings extends Settings { public void foo() { // Do your thing }}public class SingletonHolder <Type> { private final <Type> instance; public SingletonHolder(String property) { this.instance = Class.forName(System.getProperty(property)).newInstance(); } public Type getInstance() { return instance; }}[/source]

For the sake of brevity, I didn't write the exception handling code, and the SingletonHolder should be aware of changings on the value of the system property used to fetch the implementation's class name.

PARTNERS