Results 1 to 5 of 5
  1. #1
    lvh
    lvh is offline Member
    Join Date
    Apr 2008
    Posts
    3
    Rep Power
    0

    Exclamation 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.

  2. #2
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    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 @ [www.littletutorials.com]
    Language is froth on the surface of thought

  3. #3
    lvh
    lvh is offline Member
    Join Date
    Apr 2008
    Posts
    3
    Rep Power
    0

    Default

    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 :p

    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 10:14 AM.

  4. #4
    lvh
    lvh is offline Member
    Join Date
    Apr 2008
    Posts
    3
    Rep Power
    0

    Default

    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.

  5. #5
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    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 @ [www.littletutorials.com]
    Language is froth on the surface of thought

Similar Threads

  1. i need an example of JSR179 ((Location based Ser)implementation for CDC based device
    By talk_to_vivekmishra in forum CDC and Personal Profile
    Replies: 3
    Last Post: 12-30-2010, 10:07 AM
  2. Creating an array of nonprimitive objects
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:46 PM
  3. How to parse String effectively based on a dilimiter
    By raghu408 in forum Advanced Java
    Replies: 26
    Last Post: 04-10-2008, 10:55 PM
  4. creating a text based game
    By Phobos0001 in forum New To Java
    Replies: 1
    Last Post: 02-12-2008, 04:35 PM
  5. Creating objects question
    By sergm in forum New To Java
    Replies: 2
    Last Post: 12-27-2007, 04:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •