Regarding the following code, i''d suggest this is the type of thing you want to avoid. Because what happens when a new GameObject type is added to the system. This is exactly what virtual functions are for.
I''m no Java expert, so I here''s the example in C++
|
The idea here is that DoAction() is a ''message'' on a GameObject. This translates as a public member function. Each GameObject class can override DoAction().
What you are looking for is a type of double-dispatch.
Where the method invoked is dependendent upon two dynamic types. One is the type if this ( or self or the object on the lhs ) and the other is a parameter of the method.
You can accomplish this in C++ via overloading the DoAction() method.
Meyer''s Effective C++ and/or More Effective C++ provide examples of how to do this more correctly than my poor example. I''m not sure if the idea translates to Java, but it is based on overloaded methods, so if Java has them, you should be able to do this.