1

I'm working with an abstract class and a (growing) set of subclasses of this class. For certain reasons all subclasses of A should implement the singleton pattern. During startup of the application there exists a List<Class<? extends A>> and I would like to initialize all singleton-instances.

From what I see, my option here is to go for Reflection, enforcing by guidelines that all A-implementing classes have to have a defined constructor and invoke that via o.getDeclaredConstructor().newInstance();.

I've also tried a different approach where I used overloading of static methods. That is, A defined a static initialize method and all subclasses had to reimplement that method. The call can again be invoked via reflection.

The first approaches has the obvious disadvantage that violations of the programming-guideline result only in runtime-errors, not in compile-time errors. The second is even worse, if a subclass does not implement the static method, only the method in A is called with no apparent exception thrown.

So: How do I enforce a uniform way to initialize in a set of singleton classes?

Edit:

A configuration-class generates a list of all children of A during startup, those classes can be either registered directly in the configuration class programmatically or configured via a configurationfile:

private void initModules() {
  Configurator.addModule("modulename", SubOfA.class);
  ...
}

private void initModuleFile() {
  ...
  String name = in.readLine();
  String classname = ...;
  String modulename = ...;
  Configurator.addModule(modulename, Class.forName(classname));
}
Jonathan
  • 2,698
  • 24
  • 37
  • _During startup of the application there exists a List> and I would like to initialize all singleton-instances_ <-- This is not clear to me, could you also give more detail with some scheme or even simple code? – Mr.Eddart Feb 02 '12 at 12:03
  • You might also seriously consider whether a singleton is the right way to go. see http://stackoverflow.com/a/228380/151502 . Is anyone else going to see this code? – Dogmatixed Feb 02 '12 at 12:52
  • The singleton-classes are adapters establishing db-access to different databases. According to the cited answers, this would qualify as an acceptable use of singletons, as it synchronizes access to an external resource. – Jonathan Feb 02 '12 at 13:00

1 Answers1

1

those classes can be [...] configured via a configurationfile

In this case the common approach is like your first proposal: Class.forName("...").newInstance()

(This can, of course, be encapsulated by some kind of factory pattern.)

Since, in Java, you cannot enforce a particular constructor to be implemented by subclasses, the only viable way is to demand a default constructor by design guidelines/contracts.

As an example, have a look at Android's Parcelable:

Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.Creator interface.

JimmyB
  • 12,101
  • 2
  • 28
  • 44