Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annotations for Java enum singleton

As Bloch states in Item 3 ("Enforce the singleton property with a private constructor or an enum type") of Effective Java 2nd Edition, a single-element enum type is the best way to implement a singleton. Unfortunately the old private constructor pattern is still very widespread and entrenched, to the point that many developers don't understand what I'm doing when I create enum singletons.

A simple // Enum Singleton comment above the class declaration helps, but it still leaves open the possibility that another programmer could come along later and add a second constant to the enum, breaking the singleton property. For all the problems that the private constructor approach has, in my opinion it is somewhat more self-documenting than an enum singleton.

I think what I need is an annotation which both states that the enum type is a singleton and ensures at compile-time that only one constant is ever added to the enum. Something like this:

@EnumSingleton // Annotation complains if > 1 enum element on EnumSingleton
public enum EnumSingleton {
   INSTANCE;
}

Has anyone run across such an annotation for standard Java in public libraries anywhere? Or is what I'm asking for impossible under Java's current annotation system?

UPDATE

One workaround I'm using, at least until I decide to actually bother with rolling my own annotations, is to put @SuppressWarnings("UnusedDeclaration") directly in front of the INSTANCE field. It does a decent job of making the code look distinct from a straightforward enum type.

like image 914
Andrew Bissell Avatar asked Mar 23 '13 23:03

Andrew Bissell


People also ask

Can we use enum as Singleton Java?

Enum Singletons are new ways of using Enum with only one instance to implement the Singleton pattern in Java. While there has been a Singleton pattern in Java for a long time, Enum Singletons are a comparatively recent term and in use since the implementation of Enum as a keyword and function from Java 5 onwards.

How would you implement Singleton with enum?

The singleton pattern restricts the instantiation of a class to one object. INSTANCE is a public static final field that represents the enum instance. We can get the instance of the class with the EnumSingleton. INSTANCE but it is better to encapsulate it in a getter in case if we want to change the implementation.

What is Singleton annotation?

What is singleton? Singleton Pattern in android. A single instance of a class providing a global point of access to itself during the entire application's lifetime. @Singleton annotation in Dagger. A single instance of a class which is unique to a specific component, its access is limited to the scope of the component.

Is enum singleton thread safe?

It is simple to write Enum Singletons. Enums are inherently serializable. No problems of reflection occur. It is thread-safe to create enum instances.


1 Answers

You can use something like this -

public class SingletonClass {
    private SingletonClass() {
        // block external instantiation
    }

    public static enum SingletonFactory {
        INSTANCE {
            public SingletonClass getInstance() {
                return instance;
            }
        };

        private static SingletonClass instance = new SingletonClass();
        private SingletonFactory() {
        }

        public abstract SingletonClass getInstance();
    }
}

And you can access in some other class as -

SingletonClass.SingletonFactory.INSTANCE.getInstance();
like image 94
devang Avatar answered Oct 19 '22 12:10

devang