-1

While implementing one of my modules, I needed a singleton for one of my classes, say, ModuleManager. Instead of creating a class with singleton criteria, I created an interface ModuleManager to define the API and implemented this interface with an anonymous class so that there cannot be any other implementation or no other instance can be created! Can this singleton still be broken? Am I misinterpreting this as singleton? may be but it lets me to have only one instance of ModuleManager.

public interface ModuleManager{
     void manage();
 }
 public ModuleManager mgr = new ModuleManager() {
    public void manage() { s.o.p("manage"); }
 }
  • 4
    Questions about software development make much better questions if you can tie them to a specific, practical problem you're trying to solve. – Robert Harvey Dec 2 '16 at 18:59
  • @TulainsCórdova code does compile; you may have as many implementations as possible but not more than one instance of a particular implementation because this is an anonymous class. – Tech Junkie Dec 2 '16 at 19:07
  • This... is a really different way of coding something! You definitely don't have a singleton as in the usual sense, certainly, but you have something that I'm not used to see! – T. Sar Dec 2 '16 at 19:28
  • 4
    You could do mgr.getClass().newInstance() to create a new instance of the same class. What are you trying to achieve anyway? Wanting to create a singleton named SomethingManager raises a huge red flag in my head. – Vincent Savard Dec 2 '16 at 19:38
  • 1
    I suggest to declare mgr with Java's "public static final", to avoid confusion seen in answers. – Basilevs Dec 4 '16 at 19:22
0

First of all, there can be other implementations of that interface. Nothing in your code would prevent people from implementing that interface multiple times.

I would rather suggest to use an enum to implement that interface. As the JVM guarantees you that enum singletons are fully "correct singletons" out of the box. I do it typically like this:

interface FooFunctionality {
  void foo();
}

class FooFunctionalityImpl implements FooFunctionality {...

enum FooFunctionalityProvider implements FooFunctionality {
  INSTANCE;
  private final FooFunctionality delegatee = new FooFunctionalityImpl();

  @Override
  void foo() {
    delegatee.foo()
  }

That helps separating the different aspects clearly:

  1. There is an impl for the interface
  2. There is an enum for making it a singleton

And it also allows for almost complete unit-testing of all aspects in a clean, "separated" manner, too.

But of course: you can have multiple impl classes in my solution as well. And that isn't really a problem.

  • Thanks for your answer; I know about enum but was looking for smart singleton but Vincent Sarvard pointed me that another instance could easily be created! – Tech Junkie Dec 3 '16 at 2:35
  • Excatly. Singleton is about two things: constraining class instanciations and unique instance available. – Laiv Dec 4 '16 at 18:33
  • Why does interface matter here at all? The class should be in a single instance, and it is. – Basilevs Dec 4 '16 at 19:17
  • Given the context and enum suggestion, I am marking this one as the one answered my post – Tech Junkie Dec 6 '16 at 10:38
4

Acording to that rationale, every anonymous class is a singleton, since they have no name, you cannot instantiate it more than once.

One issue is that they have to live inside another class.

IMHO that is not an singleton. You can instantiate the class that contains it twice and though it's is true that the anonymous class is not the same in both cases and so cannot be called two instances of the same class, at the end you are having the same functionality in two places. So imagine the purpose of the singleton was having a centralized point to construct some resource. As you can instantiate the wrapper class as many times as you want, each one having a public anonymous nested class with a public method, you no longer have an unique point for the behavior defeating the purpose of a singleton.

  • +1 Could you please also expand your answer to a case when mgr variable is a global constant? Like "public static final" in Java? – Basilevs Dec 4 '16 at 19:26
  • @Basilevs I think that in that case OP solution could work as a real singleton. – Tulains Córdova Dec 4 '16 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.