to understand, if you could help i would be greatly appreciated :)
I have a Parent Class <Piece>, this class has Child classes, some of them being: <King>, <Queen>, <Bishop>...
Now i read each piece by a ID from a TXT File, when reading the file i want to call the appropiate Class to create a new object. (I think i didnt explain myself 😅 ), my question is how do i make that appropiate call? Should i do a:
if (id == 1) { // create new King }
else if (if == 2) { // create new Queen }
.....
What do you guys think?
Please format the code you posted, by wrapping it in triple backticks. -> `
I would use enum for chess pieces. And map pieceID->enum object. Imho, it's more natural to represent fixed enumeration of immutable objects as enum. When deserializing, simply get piece from map like pieces[id]. But it depends on what information your piece objects hold. If they should have state. Then I would also used a map but with factories. Like: pieceFactoriesMap[id].create(). Using factories is more ioc friendly and flexible.
Обсуждают сегодня