Kao
Kao

Reputation: 7364

Java: singleton from base class

Lets assume we've got base class:

public class Base {
      public Base(int i) {
      /* ... */
      }
      /* ... */
}

We want to create singleton based on this class. The problem is that class have public constructor, so extending it will result in singleton having public constructor, too:

public class SingletonBase extends Base {
   private static final SingletonBase _instance = new SingletonBase();

   private SingletonBase() {
      super(0);
   }

  public SingletonBase(int i) {  // Want to avoid this!
     super(i) 
  }

  public static void getInstance() {
     return _instance;
  }
  /* ... */       
}

Singleton, of course, cannot have public constructors, so we want to avoid this. So, what is most elegant way/pattern to have proper Singleton with functionalities from class Base?

Edit: Modifying Base class is prohibited, since it is from an external library.

Upvotes: 3

Views: 4779

Answers (3)

Luc DUZAN
Luc DUZAN

Reputation: 1319

It is not because you inherit from a class that have a public constructor that you have to create the same public constructor, you can do :

public class SingletonBase extends Base {
   private static final SingletonBase _instance = new SingletonBase();

   private SingletonBase() {
      super(0);
   }

  public static void getInstance() {
     return _instance;
  }
  /* ... */       
}

Or even :

public class SingletonBase extends Base {
   private static final SingletonBase _instance = new SingletonBase();

   private SingletonBase() {
      super(0);
   }

  private SingletonBase(int i) {  // Want to avoid this!
     super(i) 
  }

  public static void getInstance() {
     return _instance;
  }
  /* ... */       
}

Upvotes: 0

quantumbyte
quantumbyte

Reputation: 553

You could make SingletonBase just a normal subclass and delegate the Singleton-part to a different class. I don't know what your specific context is, but you would have Subclass extends Base and somewhere else you would have your SingletonContainer or something like that where you have the method public static Subclass getInstance().

That way, you would have your usual inheritance and also the Singleton effect, since the SingletonContainer would be responsible for keeping only a single instance of Subclass.

Upvotes: 0

peter.petrov
peter.petrov

Reputation: 39457

Constructors are not inherited. So just
delete this part and you'll get what you need.

public SingletonBase(int i) {  // Want to avoid this!
    super(i) 
}

Additionally, you may want to make the Base class
abstract so that no one can instantiate it by mistake
by calling the Base constructor directly.

public abstract class Base {
    public Base(int i) {
        /* ... */
    }
    /* ... */
}

public class SingletonBase extends Base {
    private static final SingletonBase _instance = new SingletonBase();

    private SingletonBase() {
        super(0);
    }

    public static SingletonBase getInstance() {
        return _instance;
    }
    /* ... */
}

Upvotes: 4

Related Questions