0

Recently, when I ask how to make methods thread-safe in singleton pattern, someone told me that using an enum version singleton pattern is a good option. and by multiple threads. If the method has side effects (changes the state of some variable) then you need to think about protecting it (make it synchronized) or parts thereof. So I write code like this:

public enum ReStation {
    INSTANCE;  

    private List<Email> emailList;

    private ReStation() {
        emailList = new ArrayList<>();
    }

    public synchronized void recycleEmail(Email email) {
        System.out.println("Recycle station recycleing the email: "
                        + email.getContent());
        emailList.add(email);
    }

    public synchronized void deleteEmail(Email email) {
        emailList.remove(email);
    }

    public synchronized void clear() {
        emailList.clear();
    }
}

however, when I read the book named "Design Pattern-Elements of Reusable Object-Oriented Software", I come across such a paragraph as below :

Applicability
Use the Singleton pattern when
• there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point.
• when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

Given an enum can't be extended, I am really confused about How could I use an extended instance without modifying their code while using an enum version singleton pattern? is modifying singleton class codes the only way to extend the functionality of the singleton?

xun yan
  • 69
  • 5
  • It's not really a singleton if you can extend it. – shmosel Apr 17 '19 at 01:53
  • I thought the whole purpose of a singleton was that it could be extended, otherwise we'd just use a static class? –  Apr 17 '19 at 02:11
  • I'm no professional coder, but I think they mean you extend it, override a method, call super.method() to reuse the old factory method (the meat and potatoes), and then you can do things like dependency injections or what-have-you-else as an application goes through it's life cycle and requires updates etc? –  Apr 17 '19 at 02:21
  • @shmosel is modifying singleton class codes the only way to extend the functionality of the singleton? – xun yan Apr 17 '19 at 02:43
  • That is a peculiar quote. I am having trouble understanding it without more context. My guess is that it was preceded by something along the lines of, “You wouldn’t want to use an enum…” Meaning, you might initially code ReStation as a non-enum class, so you can write `public static final INSTANCE = new ReStation();` while having the option to later change it to `public static final INSTANCE = new SpecializedReStation();` without breaking any code which uses INSTANCE. – VGR Apr 17 '19 at 03:10

1 Answers1

2

When the quote says the "sole instance should be extensible by subclassing", they are talking about situations where:

  • You need a single distinguished instance of a base class or interface, with a well-known access point, like the process Logger;

  • You need to choose the concrete implementation at runtime, for example based on configuration or other runtime information. Your process Logger, for example, could be implemented by FileLogger or ConsoleLogger. Usually, it should be possible to use any Logger subclass to implement the system logger.

You can't do this with the "enum version singleton pattern".

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87