So, in a simple game engine, I use the "Updated GameElement" interface to signal that an object having this interface should be updated every frame through the implementation of the update () method.
Now in my "main" (not main) class, I repeat the GameElement list and check if they are an instance of the updated GameElement. If so, I drop them and then call .update ().
Now, I just read that using instanceof is usually a sign of bad coding; and in those cases when classes are used as markers, when they can be easily replaced with a variable, I agree. But I am not very sure in my case.
I assume that I can let the GameElement class implement UpdateGameElement and define a standard empty update () method that needs to be redefined in order to actually do something, but I'm not sure if and why it will be better than mine Now.
What would you say?
Edit: some code from my main class:
public void process()
{
if (active)
{
for (GameElement GE: elements)
{
if (!GE.isToBeRemoved())
{
if (GE instanceof UpdatedGameElement)
{
((UpdatedGameElement) GE).update();
}
}
else
{
prepareRemoval(GE);
}
}
processRemovals();
}
}
source
share