Quite A Design Problem...!!

Dear Forum,

I am facing design problem...here it goes...

I have a class that contains several method having name pattern like "createXXX()".

All of these methods contains similar logic except data.This class is examined by

the another class for the"createXXX" methods for futher processing.Suppose name of class containing createXXX method is "abc"..then i have to pass this class to third party class like

addClass(abc.class);

Now to avoid almost redundant code and facilites maintenace i want to keep all my data in the xml file and "createXXX" method at the runtime in the abc class using data from xml file.

How can i make this happen ?

Thanks in advance ..!!!

[720 byte] By [AshuDaGr8a] at [2007-10-3 0:29:04]
# 1

> to avoid almost redundant code and facilites

> maintenace i want to keep all my data in the xml

> file and "createXXX" method at the runtime in the

> abc class using data from xml file.

I haven't understood the interactions between your classes, you seem to be at a point where you already know you need to generate code or compiled code, but I'm not sure why.

One way to generate compiled code using Java code is to generate classes on the fly, using reflection and/or a bytecode manipulation library. Quite an endeavour...

Another way using Java code is to embed a scripting library for ajava-ike script language (e.g. BeanShell) and generate the script on the fly...

But trying to solve this in Java language is probably more complex than using a plain source code generation tool (seems to be your approach anyway).

The simplest of which would be, if your dev env already includes it, Ant.

Make a template class with placeholders for the types, and create as many java files using Ant's <replace> target before the actual compilation target.

However there are other, more elaborate, code generation tools.

Eventually, note that you maybe don't need to generate code: a third way using Java code (with 1.5/5.0 onwards) could be to write the algorithm using generics. You're not specific enough so that we can evaluate whether your design lends itself to generification... Or maybe careful inheritance and pattern could solve your issue (that you haven't stated cleraly enough so far), without any generics...

jdupreza at 2007-7-14 17:22:00 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

tr y this

create a Interface which contains the similar method

Implement this interface for all the classes where u have that method

at runtime pass the class name which u want to create a instance

such as

factoryClass="name of the class";

Class aClass = Class.forName(factoryClass);

IInterface instance =null;

//get constructors by reflection

Constructor aConstructor = aClass.getConstructors()[0];

//create a new instance

Object []initArgs = null;

Object obj = aConstructor.newInstance(initArgs);

//type cast to Interface

instance =(IInterface) obj;

instance.createXXX();

do this help u, to solve ur problem

harish.suna at 2007-7-14 17:22:00 > top of Java-index,Other Topics,Patterns & OO Design...