Help with interfaces/ inheritance
Hello im building a game where I'll have a board and GamePiece's that will be placed on the board,
I have tokens, these can be placed on the board after some validation and they can't be moved once placed for the rest of the game
I have totems, these will be automatically be placed in the center at the start of the game and they can be moved afterwards
I need a way to distinguish between them so:
1- Inheritance ? Bad choice of using inheritance in here because I'm not needing hierarchical use of methods in gamepiece it would only be to extends from GamePiece and call the super constructor in the constructor for both cases, so im only using it for subtyping, which is bad practice
I don't know what other option there might be for this, any help designing this ?
9 Replies
⌛
This post has been reserved for your question.
Hey @userexit! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
Sealed classes.
Have a kind property in GamePiece, or whatever you can name it.
Type this property as an enum that can be TOKEN, TOTEM or whatever else.
Store your tokens and your totems in separate containers. Which is where specific subtyping becomes legitimate anyway. You'll need like a List<Token> and a List<Totem>, that's directly the point of typing (albeit not inheritance so much)
You need polimorphysm and hierarchical use of data for inheritance to come in discussion
But not for proving that subtyping is actually not a bad practice. It does typing.
I didn't understand this
Having a List<Token> to contains only tokens and a List<Totem> to contain only totems is good, not bad. Having types that make it so you can do that is good, not bad.
Are you sure
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.