×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Oct 2003
    Location
    Bangladesh
    Posts
    41

    Class Design Problem

    i know mobile has limited memory, and heap size is a big factor to consider when designing games.
    now let me ask my question through an example:

    i have two enemies, one that runs and other one runs and jumps.
    now they have common characteristics, so i can use a single class to code these enemies,
    and use logics to differentiate them.

    again, i can use two classes for each of them.

    Now my question is which approach should be better and efficient in terms of memory usage?

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    There's no definitive answer... it depends on the classes, and how they differ. But in general, one large class will be more efficient that two small ones.

    There is an overhead per class (both from the file itself, and the index entry in the JAR). There is an overhead in the class for each method (from the method name and various data structures that describe the method). You can get some idea of the size of these by compiling an empty class (with no methods), then compiling a class with one method (that contains no code).

    So a class with a few large methods is more efficient than a class with many small methods. Interfaces are particularly inefficient, because they contain only names and internal data structures, and no code.

    If you go for the single class option, then presumably you would have some logic like:
    Code:
    if (isJumpingEnemy) {
        // do jumping enemy stuff
    }
    This takes space too, but very little.

    If you had an enormous number of these blocks, then using separate classes might manage to be more efficient... but I doubt it. Even a very small class compiles to a few hundred bytes, and you can get a lot of "if" blocks for that.

    Graham.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×