简体   繁体   中英

Java board game real-time server, should I use immutability or not?

I have been thinking a long time on how to conciliate those 2 requirements for a board game server that notify players in real-time:

  • I want to send events to the players (an action in a boardgame can have multiple consequences, I want to animate what happens without re-implementing all the logic on client side).
  • I want to expose the state of the game (when a player reloads, I need to send the current state of the game, without replaying all the events).

First requirement tells me "make an mutable observable object". Second requirement tells me "make an immutable object" to expose state safely.

Which model would you choose to implement the rules? Mutable or Immutable?

Replaying games, connotating single steps, computer algorithms (undo, tries), would favor immutable objects.

That then you want to send the current snapshot to a newly entering user, is a secundary issue: keep a current snapshot, entire picture around.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM