10

In the question What is an efficient way to implement a singleton pattern in Java? the answer with the most upvotes says, to use a Enum for implementing a singleton.

That is fine and I understand the arguments, respectively the language advantages.

I have, however, a set of classes which I define singleton but which need to extend other classes, this is not possible with the enum approach, since enums cannot subclass.

Joshua Bloch says in his slides:

  • But one thing is missing—you can’t extend an enum type
    • In most cases, you shouldn’t
    • One compelling use case—operation codes

In most cases you shouldn't: could someone elaborate on that? I have implemented several servlets and they extend HttpServlet, why shouldn't these be singletons? I only want one instance of them in my application.

Community
  • 1
  • 1
Mahoni
  • 7,088
  • 17
  • 58
  • 115
  • Please could you explain why you want a singleton servlet (what are the constraints that led to this choice)? – Romski Jul 12 '12 at 14:20
  • @Romski The application only needs one instance of this servlet. – Mahoni Jul 12 '12 at 14:36
  • enum can implement interfaces. With delegation this means you have only one instance which use functionality defined in another class. – Peter Lawrey Jul 12 '12 at 18:01

3 Answers3

7

A Singleton class can extend other classes; actually by default in Java it would anyway extend Object. However what Josh is referring to is that you shouldn't extend a Singleton class because once you extend it, there is more than 1 instance present.

Answering the comment:

Actually the best way to implement the Singleton is:

From Effective Java

// Singleton with static factory
public class Elvis {
private static final Elvis INSTANCE = new Elvis();
private Elvis() { ... }
public static Elvis getInstance() { return INSTANCE; }
public void leaveTheBuilding() { ... }
}

Here Elvis can extend any other class.

user1168577
  • 1,863
  • 11
  • 14
  • And how does that help me in implementing my servlet with the enum idiom? A Singleton class implemented using the enum type, **cannot** extend other classes. – Mahoni Jul 12 '12 at 13:46
  • No, an online portion of "Effective Java" says to the enum approach. >> This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. While this approach has yet to be widely adopted, a **single-element enum type is the best way to implement a singleton** < – Mahoni Jul 12 '12 at 13:53
  • 2
    Maybe I should have said, one of the better ways. But for you this would be the best way because you want to extend. – user1168577 Jul 12 '12 at 13:56
3

You shouldn't care about the actual instance(s) of your servlets - lifecycle management is handled by the servlet container according to the Servlet specification contract to which you have agreed. If it makes sense to implement parts of you server-side functionality as a singleton, then go ahead and do that any way you like and use it from your servlet.

Gustav Barkefors
  • 5,016
  • 26
  • 30
  • This question solved my problem, but since my question was more general I decided to check the other one as excepted. Thanks anyway! – Mahoni Jul 12 '12 at 20:22
1

Josh is referring to extending the enum type, not to having the singleton type extend something else.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • But I cannot implement my servlet with the enum singleton idiom, since this way it cannot subclass `HttpServlet`: `public enum MyServlet extends HttpServlet` is not allowed. I don't want to extend my singleton, I want to let my singleton extend another class. – Mahoni Jul 12 '12 at 13:42
  • Yes, clearly. What I'm saying is that Josh wasn't addressing that issue in this quote ;) It's clear that you can't use the enum idiom in this case, so what's the problem? – Louis Wasserman Jul 12 '12 at 14:11