View Single Post
Old 12-25-2011, 04:33 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Singleton is designed for the instance of that class. I think that the call against a .newInstance off the class loader would chain to the constructor, so a private or protected constructor will throw an exception.
To load, its a simple call to Singleton.getInstance();. There is really no way to do this dynamically since you cannot stop an class with a public constructor from instantiating a new record. Any class to be a singleton has to be an instance of a Singleton first. You can call it against a dynamically loaded class though, that's just a simple try/catch and attempt to invoke the Method of getInstance on the class or cast to a Singleton type.

So if I got what you are looking for, your dynamically loaded classes implement singleton. You can load them as so:
PHP Code:
try
{
    
Method getInstance = class.getClassLoader().loadClass("MyClass").getMethod("getInstance");
    
Object instance getInstance.invoke();
}
catch (
Exception ex)
{
    
System.out.println(ex.getMessage());

Methinks that will work.
Fou-Lu is offline   Reply With Quote