Game Development Reference
In-Depth Information
accompanies this topic, the GameCodeApp class is probably the one that comes clos-
est to this, but if you study it a bit you can find some easy ways to factor it.
When I was working on The Sims Medieval, there was a class that fell very neatly into
the Blob category. Our Sim class became a dumping ground for every little extra
timer, variable, and tracking bit that could be remotely tied to a Sim. Entire systems
would be written inside this one class. By the end of the project, the Sim.cs file was
11,491 lines of code, and it was nearly impossible to find anything.
I try always to use a flat class hierarchy. Whenever possible, it starts with an interface
class and has at most two or three levels of inheritance. This class design is usually
much easier to work with and understand. Any change in the base class propagates
to a smaller number of child classes, and the entire architecture is something normal
humans can follow.
Try to learn from my mistakes. Good class architecture is not like a Swiss Army
knife; it should be more like a well-balanced throwing knife.
Inheritance Versus Composition
Game programmers love to debate the topics of inheritance and composition. Inheritance
is used when an object has evolved from another object, or when a child object is aver-
sion of the parent object. Composition is used when an object is composed of multiple
discrete components, or when an aggregate object has a version of the contained object.
A good example of this relationship is found in user interface code. You might have a
base control class to handle things like mouse and keyboard events, positioning, and
anything else that all controls need to know how to do. When you create a control
such as a button or check box, you will inherit from this control. A check box is a
control. Then you might create a window that can contain a bunch of these controls.
The window has a control or, in this case, many controls. You window is most likely
a valid UI control as well, so it might be fair to say that that your window is a con-
trol, too. When you make a choice about inheritance or composition, your goal is to
communicate the right message to other programmers. The resulting assembly code
is almost exactly the same, barring the oddities of virtual function tables. This means
that the CPU doesn
t give a damn if you inherit or compose. Your fellow program-
mers will care, so try to be careful and clear.
'
Virtual Functions Gone Bad
Virtual functions are powerful creatures that are often abused. Programmers often
create virtual functions when they don
'
t need them, or they create long chains of
 
 
 
Search WWH ::




Custom Search