I'am trying to create an UML class diagram for a simple game. I've three inheritance classes (NPC
, Player
, Monster
) and they should interact with each other (e.g. in an attack).
I wonder if I should use interfaces in my simple case. Also how can I expand my diagramm?
Your class Character
is specialized into: NPC
(Non-Player Character), Player
and Monster
and you wonder if you'd need an interface:
Monster
seems to be a non-player character, it should probably inherit from NPC
instead of Character
Player
and the corresponding Character
. So you'd have a simple association between the two and not an inheritance. An immediate advantage, is that the player could chose the preferred character to impersonate him/her.Colleague
interface, and let different classes implement this interface. But if your Colleagues
are necessarily all Characters
, you could just rely on the superclass as you did.More generally, an additional interface is a proven approach if you want to decouple classes. You should definitively consider them if you'd develop a game engine that is to be reused in a lot of different games: you'd then have an engine that relies only interfaces that are independent of any specific game. Each game would then pick the relevant classes to implement the interfaces. But for your specific case, it seems to be an overkill.
This being said, the main challenge you'll be confronted with is that you'll end up with deep class hierarchies that'll be difficult to evolve. This is why, the game industry prefers the entity-component-system pattern that prefer composition over inheritance. But this is a different story, and there are full books on that topic ;-)