I'm trying to structure my game object classes similar to how it is done in this article. One way to implement this strategy is discussed in this StackOverflow question.

Following the strategy proposed in the linked question above, how would I control how objects are handled based on their implementations?

For example: suppose I have a base class Item and two subclasses ItemPhys and ItemStatic. These two subclasses both extend Item. ItemPhys implements the interface Moveable, but ItemStatic does not.

If all of the Item's in my game world are stored in a single ArrayList of Item objects, how do I make it so Item::update() will only try to call Item::updateMovable() if the Item in question implements Moveable?


1 Answer 1


Beware, so that you don't create any tight coupling you are trying to avoid by using this kind of object hierarchy.

I'd suggest you use a uniform way of updating all the components. Let all your entities (e.g. ItemMovable and ItemStatic) implement an Item::Update() method. Then call Update on every entity in your system. Do what you have to do for Movable ones and leave the implementation of the ItemStatic::Update() empty.

If you're worried about performance (i.e. it turns out to be slow specifically because you're calling too many empty methods), you could keep two lists of entities; one that contains all the entities with meaningful Update() method implementations, and one for the rest.

  • \$\begingroup\$ This sounds like a great idea! \$\endgroup\$
    – Kyle V.
    Commented Feb 2, 2012 at 17:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .