Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-30-2008, 12:38 AM
lvh lvh is offline
Member
 
Join Date: Apr 2008
Posts: 3
lvh is on a distinguished road
Creating objects based on a String value
Hiya

Short problem sketch: I have a configuration file I'm parsing. This config file describes an object per line of text. I can read from the config file without problem.

Each line should result in a new object. All of these objects have a common (abstract) superclass, Furnishing. There are a number of subclasses describing the stuff I see in my living room: Couch, TV, Chair, Table...
.
Right now, I have something that _works_ (more or less) but is simply horribly poor code.

I've pastebinned some of this code. (Please note, I know this is horribly ugly and bad -- that's why I'm complaining):
Nopaste - No description


Issues:
  1. (worst) This method needs to know the gory details of my object model. If I change something there, I have to change it here. Right now it makes two classes -- but I could easily end up having ten, or so. That's a lot of stupid, repetitve code. (see also 3). This means horribly poor coupling b
    etween my IO class and my internal objects.
  2. (ugly) I have to instantiate the Furnishing F (second line) with a dummy Aquarium object that does nothing, because else the code won't compile (because there is no guarantee that f will be instantiated by the time I return it). Should be easy to fix, I guess.
  3. (minor, I think) The limiting factor to refactoring all of this into one method IMO is the fact t that different classes need a different amount of arguments. I could fix this by simply making a constructor that accepts "int[] argumemnts" instead of each separate int.

Essentially, I need something that makes objects of a class of which the name is in a String object. Whats the Right Way(tm) to do this?

Thanks in advance.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-30-2008, 02:01 AM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 183
danielstoner is on a distinguished road
Hi LVH,

I am not sure how advanced a programmer you are because your profile doesn't mention it. I am gonna describe here a solution and if you need more guidance let me know.

Java has a very nice feature called "Reflection". For your problem this is the perfect solution. what you have to do is to create a factory class that is able to read your configuration file and based on what you have in there it will create instances of your objects.
In order to be able to do this you have to make your classes obey to some standards. First one you already have, they are all derived from a parent class or implement the same interface. This helps with the design of your factory class.
The second requirement is for your classes to have a constructor with the same signature. To keep it simple for now you can make your classes to have a no parameters constructor.
Then you have to modify your configuration file to specify the name of each class as "package.Table" or something similar.
Now when you start your program you create the factory instance with the configuration file as parameter. While you read the class names from the file you can instantiate them using reflection with the call java.lang.Class.newInstance(). Here you have to deal with a number of exceptions.

If this is not clear enough I can write a small example for you when I have 20 minutes. But I would like to see you try it first. Post the code here. Cheers.
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-30-2008, 11:05 AM
lvh lvh is offline
Member
 
Join Date: Apr 2008
Posts: 3
lvh is on a distinguished road
Quote:
Originally Posted by danielstoner View Post
Hi LVH,

I am not sure how advanced a programmer you are because your profile doesn't mention it. I am gonna describe here a solution and if you need more guidance let me know.

Java has a very nice feature called "Reflection". For your problem this is the perfect solution. what you have to do is to create a factory class that is able to read your configuration file and based on what you have in there it will create instances of your objects.
In order to be able to do this you have to make your classes obey to some standards. First one you already have, they are all derived from a parent class or implement the same interface. This helps with the design of your factory class.
The second requirement is for your classes to have a constructor with the same signature. To keep it simple for now you can make your classes to have a no parameters constructor.
Then you have to modify your configuration file to specify the name of each class as "package.Table" or something similar.
Now when you start your program you create the factory instance with the configuration file as parameter. While you read the class names from the file you can instantiate them using reflection with the call java.lang.Class.newInstance(). Here you have to deal with a number of exceptions.

If this is not clear enough I can write a small example for you when I have 20 minutes. But I would like to see you try it first. Post the code here. Cheers.
Java 101, but extensive Perl5Moose and Perl6 OO experience (and notions of Ruby/Python newstyle classes).

Right. I had found reflection myself thanks to the help of the good people at #java @ Freenode and I've gotten newInstance to work by myself.

As an exercise, I'd now like to use .getConstructor too -- but I can't get it to work (pastebin inc at end of post). I can't figure out how getConstructor works. It's a method that applies to Class objects, but somehow it expects a class parameter?! (The error I get when I try to compile is (".class expected") See the pastebin for more info on how I managed to mess it up

newInstance() doesn't work because I don't have argless constructors -- but it does throw the exception I expect it to throw -- that's a good thing I suppose

The constructors I have are of the form public SomeClass (int[] args) -- this is good because int[] args is what I get from my parser ;-)

http://pastebin.com/m6495af8c

Of course, I could just write empty constructors, use super(), and make methods in my superclasses/subclasses (depending on the amount of classes that have that particular attribute) but that is suboptimal because Chairs (x,y,radius) have less arguments than Tables (x,y, width, height) -- would be cool if I could just pass an int[] to the cnstructor and then handle it there.

We haven't seen reflection in class, but from what I've read I understand its equivalent form in other OO languages.

Last edited by lvh : 04-30-2008 at 11:14 AM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-30-2008, 12:42 PM
lvh lvh is offline
Member
 
Join Date: Apr 2008
Posts: 3
lvh is on a distinguished road
Sorry for the quick bump, but perhaps you'd like to know that I've managed to fix it myself (mostly).


http://pastebin.com/m7c499dc9

Problem (2) in my original post remains. How can I make sure Furnishing f (line 10) is always instantiated without doing something ugly like making a dummy Aquarium? Furnishing is abstract.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-30-2008, 03:00 PM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 183
danielstoner is on a distinguished road
The cruel reality is you cannot be sure. Maybe there is a typo in the config file and then the instance will not be created. The best thing is to actually treat the error. For example you can check for null: if (f != null) {furnishings.add(f);}
Exception handling is a very heated topic on the net. If you want you can also take a look at my article on the subject: Exceptional Java - Thoughts on Java exceptions | Little Tutorials
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
i need an example of JSR179 ((Location based Ser)implementation for CDC based device talk_to_vivekmishra CDC and Personal Profile 1 06-22-2008 10:45 PM
Creating an array of nonprimitive objects Java Tip java.lang 0 04-14-2008 09:46 PM
How to parse String effectively based on a dilimiter raghu408 Advanced Java 26 04-10-2008 11:55 PM
creating a text based game Phobos0001 New To Java 1 02-12-2008 05:35 PM
Creating objects question sergm New To Java 2 12-27-2007 05:10 PM


All times are GMT +3. The time now is 12:09 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org