Wednesday, December 22, 2004

Subclassing my own classes

I thought I understood reasonably well objects and classes ... and yet, I had never based one of my classes on one that I had previously created. In fact, most of my classes were not only "terminal" but they were instantiated only once in my program (with the exception of a custom dialog class). Today, it finally dawned on me how I could subclass my own classes. In RUR, I had a robot class and a world class, both of which represented only the logical part; the display was handled separately as I understood it should be. Yet, it lead to enormously interlinked code which didn't feel right.

What I will do, is subclass both the robot and world classes into something like visual_robot and visual_world, which will include all the visual information required to draw themselves. I will also include the robot object in the complete world (eventually creating a list of such objects), since the robot(s) should be part of the world just as the beepers and walls are. This should simplify tremendously the links between the window display and the objects in it.

This approach should also allow for much greater flexibility in giving different appearance, as one can concentrate all the relevant visual parameters in once place, and change them at will.


Anonymous said...

I know that this is an old post, but hey, isn't Google great?

What you've proposed with creating a subclass to contain the visual elements of the parent is not a good idea, but since this post is 4 years old, you probably already know that by now.

André said...

Google is great indeed! :-)

Actually, the design I eventually settled for was having a class for the robot "brain", and used that as a subclass for the more complete robot (with visual information such as which image to use when it was in a given orientation). In effect, it's the reverse of what I described in the post. I used that and it worked very well - plus it turned out to be very readable (I think!) I haven't touched that part of the code in about 4 years though. You can view it if you download rurple (it's in