Creating a singleton class without using a static method

I need to create a singleton class without saving a static method.

How can i do this?

+7
source share
7 answers

Create a single instance enumeration

enum Singleton { INSTANCE; private Field field = VALUE; public Value method(Arg arg) { /* some code */ } } // You can use Value v = Singleton.INSTANCE.method(arg); 

EDIT: The Java Enum Tutorial shows how to add fields and methods to an enumeration.


BTW: Often, when you can use Singleton, you really don't need one, since the utility class will do the same. An even shorter version is just

 enum Utility {; private static Field field = VALUE; public static Value method(Arg arg) { /* some code */ } } // You can use Value v = Utility.method(arg); 

Where singletones are useful when they implement an interface. This is especially useful for testing when using dependency injection. (One of the weaknesses of using a Singleton replacement or utility in unit tests)

eg.

 interface TimeService { public long currentTimeMS(); } // used when running the program in production. enum VanillaTimeService implements TimeService { INSTANCE; public long currentTimeMS() { return System.currentTimeMS(); } } // used in testing. class FixedTimeService implements TimeService { private long currentTimeMS = 0; public void currentTimeMS(long currentTimeMS) { this.currentTimeMS = currentTimeMS; } public long currentTimeMS() { return currentTimeMS; } } 

As you can see, if your code uses TimeService everywhere, you can enter either VanillaTimeService.INSTANCE or new FixedTimeService() , where you can control the time from the outside, i.e. your timestamps will be the same every time you run the test.

In short, if you don't need your singleton to implement the interface, all you might need is a utility class.

+19
source
 public class Singleton { public static final Singleton instance = new Singleton(); private Singleton() {} public void foo() {} } 

then use

 Singleton.instance.foo(); 
+8
source

Another approach is the singleton owner id , which offers on-demand initialization:

 public class Something { private Something() { } private static class LazyHolder { public static final Something INSTANCE = new Something(); } public Something getInstance() { return LazyHolder.INSTANCE; } } 

(The example is changed, the getInstance method is not static)

Note that stand-alone singletones like this should be avoided whenever possible, because it contributes to a global state, leads to hard-unittest code, and depends on one context of the class loader, to name a few possible disadvantages.

+2
source

Follow Joshua Bloch's recipe listing in Effective Java 2nd Edition. This is the best way to create a singleton.

I do not understand why this is so much. Is a single drive a discredited design pattern? GoF will vote for him from the island today.

+1
source

You can use one of the IoC containers (e.g. Google Guice ) and use your class as singleton (impatient or lazy - it depends on your needs). This is easy and flexible, since the creation of instances is controlled by the IoC framework - you do not need code changes if, for example, you decide to make your class not singleton later.

+1
source

Use factory object, extract your singleton object from this factory.

 ObjectFactory factory; .... MySingletonObject obj = factory.getInstance(MySingletonObject.class); 

Of course, there are many frameworks that will help you achieve this. spring frame is a popular choice.

0
source

Inside the constructor, you need chceck if there is an instance of the class somewhere. Therefore, you need to save the reference to the singleton instance in a static variable or class. Then in the contectoror singleton, I would always check if an existing instance of the singleton class exists. If so, I would not have done anything if I had not created it and installed the link.

0
source

All Articles