Alex’s Weblog

My personal blog

Constructor Class

Today I was trying to create na standard Factory design pattern to a bunch of classes that I created. Using JAVA reflection classes I have designed some cool stuff:

 

    protected IGenClass createObject (Class objClass) throws NoSuchMethodException, Exception{

        Constructor constructors = null;

        try {

            constructors = objClass.getConstructor();

        }catch (NoSuchMethodException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        return constructors.newInstance(sIndividual);

    }

 

In this simple case would be the same that wrote the code

IGenClass obj = new ImplGenClass();

 

I know this is not a great thing, but use some imagination, this can be a great thing to make easy to create a bunch of factories.

I hope this can be useful.

Novembro 13, 2008 Publicado por alexpinheiro | Uncategorized | | Sem comentários ainda