aspose file tools*
The moose likes Java in General and the fly likes Dynamic Object Creation...urgent help needed..! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Dynamic Object Creation...urgent help needed..!" Watch "Dynamic Object Creation...urgent help needed..!" New topic
Author

Dynamic Object Creation...urgent help needed..!

kala praveen
Greenhorn

Joined: Nov 04, 2004
Posts: 21
Greetings..!

Suppose that there are classes X,Y,Z and each of these is having a method hello(). Now, I have another class A which is having a method
sayHello(String args). Now, I will send any of the three class names to the above method as a String and want to create that particular object and call hello method on it.

for Eg:

If I call new A().sayHello("x"), object of x must be created and x.hello() must be called. the same thing shud happen with y and z..

How to implement this method?

I just took 3 classes x y z 4 timebeing. There may be more classes in some instances. So,simple if-else, or switch condition will not be a better idea I think.

Any reasearch or suggestions in this regard are appreciated.

Thank you.
Kala.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Hmm. What you are doing is a little confused. Why do you have to pass your X, Y and Z objects as a String? Why can't you pass them as generic Objects?

I think this might be what you want to do - create three classes (your X, Y and Z classes) and have them implement an interface. Define your hello() method in this interface and implement it in your classes. Now you can call your sayHello() method without caring which type of object you have. No need for a conditional.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Just in cas eyou're interested, there's a pretty good example of this in the book Head First Design Patterns

As Paul said, the first thing you need is a interface for the classes X, Y and Z to implement so we can treat them the same according to a common behaviour. I have some doubts on your method signature (why are you passing the class name as the parameter??) but we'll do it your way...

First step: if the method sayHello was passed an instance of a concrete class, eg X, it would look like this:


Second step: if we make sayHello accept the interface instead:


Third step: if we make sayHello accept a Class type and the Class implements the HelloInterface:


Fourth step: if we make sayHello accept a Class name and the Class implements the HelloInterface:


As you can see I haven't done everything for you, java.lang.Class does most of the missing stuff. Obviously given your requirements you need the fourth step, but if you read the first chapter of the book above you'll understand what I'm referring to.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

This is quite easy to do -- it's a straightforward application of the Java Reflection API. The linked tutorial has all the examples you'll need.


[Jess in Action][AskingGoodQuestions]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Well yeah, if you wanted to teach him how to fish
kala praveen
Greenhorn

Joined: Nov 04, 2004
Posts: 21
Hi All,

Thank you so much for all the responses. I will explain the scenario in a more detailed way. Please be patient and go thru it.

I have a log generator program which generates different logs very rapidly. I have to catch each log and depending on the type of a pariticualr value in it, I need to insert it into a particular table in the database. Like that, I have to insert logs into more than 40 different tables.

I have written an xml file which maps each type of the log to a particualr class name, instance of which will be resposible for inserting the log into database.

Eg:
<log-class-map>
<log-type>x</log-type>
<class-name>a</class-name>
</log-class-map>

<log-class-map>
<log-type>y</log-type>
<class-name>b</class-name>
</log-class-map>

and so on.....

So, I need to implement a program which takes the log from generator program, determines the type of it, gets the appropriate class from the XML mapping,creates an object of it and call the method insert(LogStream) on it.

Every class will have this insert method with the same signature but with a different implementation.

I could implement all the functionality except creating the Objects based on the String that I get from the XML file.

Thank you so much for your patience and help.

God is real unless declared integer...Kala.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

OK. Well, as I said, go read this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic Object Creation...urgent help needed..!