I am currently writing a library in which users can create own subclasses of a parent abstract class Module
.
So the library already contains some modules written by me, for example ModuleOne
, ModuleTwo
, ...
Each Module
- subclass should be easily identifiable with an unique identifier, for example an integer, because I want to save multiple module configurations and connections in an xml file.
So I can load the saved configurations later and instanciate objects of the desired type.
If I just use predefined classes this is easy, because I know the identifiers of ModuleOne
or ModuleTwo
, so I can create the Modules in a function:
public static Module getModuleByType(int type)
{
if (type == ModuleOne.TYPE)
return new ModuleOne();
else if (type == ModuleTwo.TYPE)
return new ModuleTwo();
//and so on...
}
But if a user makes a subclass of Module
, for example CustomModule
, how can I assign unique identifiers to this class, so that all Instances of this class have the same type identifier?
How can I dynamically recreate these Modules from their identifiers? So I cannot create the Modules in getModuleByType()
, because I do not know their constructor and their identifier.
Thank you for your help.