wwboy6手記

This is about when to create a class: for a new feature instead of a new object concept
For example, an enemy for a combat game. Instead of making a class for an enemy, we should make a class for a feature/skill/behavior of enemy. Enemy should be a “data structure” instead of “controller”.

// Wrong practice:
class EnemyA : Enemy {
    ...
    public void throwFireBall() {...}  // it is hard to share this
                                       // with other enemy
}

// Correct practice:
class Enemy {
    string name; //name = "EnemyA"
    List<EnemyBehavior> behaviors;
}
class EBThrowFireBall : EnemyBehavior { //EB stands for EnemyBehavior
    public override void someEnemyMessage() {
        //throw fire ball
    }
}


Therefore different enemies can share same behavior with different settings and combination.

The key to make this decision is all about concept: enemy is a combination of behavior, and the behavior sharing between enemies are expected.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: