dimanche 30 avril 2017

How to create a singleton using enum while also passing state of the singleton

I have created a singleton using double locking check and now I want to create a singleton using enum. I know I can create singleton something like below:

public enum MyEnumSingleton{
    MY_ENUM_SINGLETON(2, "Hello");

    private int id;
    private String name;

    MyEnumSingleton(int _id, String _name){
        this.id = _id;
        this.name = _name;
    }
}

But I want the "id" and "name" information to be passed passed dynamically and not hardcoded in enum, much like the way I can do new MySingleton(2, "s") with any class.

I have how to hardcode the value and create enum singleton but not able to get how to pass information dynamically.

Aucun commentaire:

Enregistrer un commentaire