Java subclasses - converting a superclass to a subclass

Put your problem here if it does not fit any of the other categories.

Java subclasses - converting a superclass to a subclass

Postby ameetnsharma » Thu Dec 17, 2009 2:04 am

I'm developing a game at the moment in android. There are a number of different types of graphics...

Say I have some class called GraphicObject... which has a bunch of subclasses...

What I'd like to be able to do is create a number of GraphicObjects at the start of the game (kind of like a resource pool of graphical objects)... and then change them to a subclass in the middle of the game, depending on what type of GraphicObject I need at the time...

I was wondering if this was possible... because I don't want to create new objects mid-game... that could really slow down the game...

Only other option I see is to create a pool of objects for each subclass... which seems like it should be unnecessary...

There is no dynamic memory allocation inside the subclasses... they may have an extra int or float variable...

Thanks. Appreciate any help.
ameetnsharma
Developer
Developer
 
Posts: 30
Joined: Tue Dec 08, 2009 10:55 pm

Top

Postby hardcoras » Thu Dec 17, 2009 2:50 am

You can do it with a interpreted languages like JavaScript, injecting objects with a new code. But I don't think is possible with binary code like in Java or C++.
Maybe there is some kind of ways to change bytecode in the memory on JVM. But I think it won't be enough to play with classloaders or smt, but you'll need to change even JVM behaviour. And taking into mind that in Android isn't regular JVM, but dalvik runing dex (risc architecture), i think this option it's not even worth a consideration.
Maybe i'm mistaken and there is some cool technology, but i'm not aware of it.

I prefer "to favor composition over inheritance" (by the Gang of Four). Maybe you should consider it also.

Good luck.

Readings:
http://replicaisland.blogspot.com/2009/ ... nents.html
hardcoras
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Sat Nov 14, 2009 2:31 pm
Location: Lithunia

Postby ameetnsharma » Thu Dec 17, 2009 3:23 am

Thanks harcoras! That's a very interesting approach. The thing that comes to mind for me is... doesn't the GameObject needs to have all the fields necessary so that any of the GameComponents can manipulate it appropriately? It does seem like a very small price to play though for the advantages...
ameetnsharma
Developer
Developer
 
Posts: 30
Joined: Tue Dec 08, 2009 10:55 pm

Postby hardcoras » Thu Dec 17, 2009 1:55 pm

To say the truth I haven't read the blog post. And i have some other interesting readings to do. :D
But as i understand from class names, GameObject is a class aggregating GameComponents. In this case I don't think you need to put all fields into GameObject, GameComponents can contain them. Maybe you could link GameComponents with each other, i don't know. Just take some time to rethink of your game design (code structure).

By the way I recommend to read whole Chris Pruett's blog http://replicaisland.blogspot.com/ . I think there should be great ideas, as i sad i havent read it myself. But i heard Chris Pruett is good games developer, but don't know if he is a good blogger.
hardcoras
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Sat Nov 14, 2009 2:31 pm
Location: Lithunia

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 24 guests