0

I am currently writing a program in which about 12 classes need to be a singleton, due to that they are using a messaging service which needs different types. My question is, instead of basically copy and pasting the singleton code for each for creating an instance with only changing the class it makes an instance of. Is there someway to have a common code that is used for the singleton pattern, for any class that needs to create a singleton?

Here is the code to create one of the singletons,

public static void create()
{
    if(instance == null)
    {
        instance = new FooDataWriter();
    }
}
jgr208
  • 2,896
  • 9
  • 36
  • 64
  • Don't be offended, but if you have 12 singletons, you are probably doing something wrong with your design. – Henrique Barcelos Apr 10 '15 at 15:28
  • @HenriqueBarcelos sadly we are not, we are using RTI DDS and each message type is of its own type. So like A is its own type and B is its own type. Therefore we want to make a singleton of each type so we don't have a bunch of objects made when not needed. – jgr208 Apr 13 '15 at 12:43
  • Well, I disagree. If you might be interested, take a look on the [Flyweight](http://en.wikipedia.org/wiki/Flyweight_pattern) pattern – Henrique Barcelos Apr 15 '15 at 01:19

2 Answers2

2

You have to copy and paste whatever code you are using to implement the singleton, but according to Effective Java (2nd edition, p.18) the best way to enforce a singleton is to use a one-element enum:

public enum MySingleton {
    INSTANCE;

    // methods
}

If you do it this way there's almost nothing to copy and paste!

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • That is also a good idea! I was just hoping there was a way to share some common library for a singleton ask to not have to change the code every where if something changes in the future but I guess there is no way around it. – jgr208 Apr 09 '15 at 14:19
  • 1
    @jgr208 I don't think there is a way around it, sadly. The drawback of using an enum is that you can't use `MySingleton extends ...`. Apparently the best way to implement a singleton that is not an enum is this: http://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom – Paul Boddington Apr 09 '15 at 14:21
0

Well something like this is possible:

public final class Singletons {

    private static final Map<Class<?>, Object> map = new HashMap<>();

    private Singletons() {
    }

    public static <T> T get(Class<T> type) {
        Object instance = map.get(type);
        if (instance != null) {
            return (T) instance;
        }
        synchronized (map) {
            instance = map.get(type);
            if (instance != null) {
                return (T) instance;
            }
            try {
                instance = type.newInstance();
            } catch (Exception e) {
                throw new IllegalArgumentException("error creating instance");
            }
            map.put(type, instance);
            return (T) instance;
        }
    }
}

Then you could do just:

FooDataWriter instance = Singletons.get(FooDataWriter.class);
Bubletan
  • 3,833
  • 6
  • 25
  • 33