I'm trying to use composition in an Entity class of mine. So far, an Entity "is" Displayable(inherited an ABC), and "has" a Position(contains a point-like class). I know I will want my Entity to "have" a/some health, so I started defining a Health class to be composed into Entity. It was super-simplistic though, and only ended up containing 2 methods: hurt, and heal that modify its single field: hp.

If I intended to inherit from Health, I guess this would be OK, as it wouldn't require any boilerplate inside of Entity; it would just inherit the methods.

If I'm going to compose with it though, so for me to hurt an Entity, I'll have to route the hurt/heal call to their Health member and call their own hurt/heal method. With a class this simple, that means I'll basically have all of Health's methods duplicated in Entity.

In a case like this, is there any point in defining Health? The simpler alternative would be to just give Entity a hp field, and give it the hurt and heal methods directly.


As long as the underlying hp value is not public, it doesn't matter too much either way. Having Entity::hurt()/heal() directly access a private hp member is fine, and making them wrappers for a Health::hurt()/heal() call is also fine.

Assuming it's not public, I would base the decision on what you expect to happen in the future. Will health probably be a simple integer forever? Or do you think it'll turn into a non-trivial class with methods like getTimesHurt(), getLastHealValue() or getPoisonTicksRemaining()? You may not know for sure, but you can make a well-educated guess based on what kind of game you're making.

  • Thanks. I didn't consider "effects" that I might need to add later. Originally, I just wanted to increment/decrement the health value, but I guess I should look at the reusable big-picture. Thanks. I'll make it into it's own class in case I want to add on to it later. – Carcigenicate Apr 18 '15 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.