Domanda

My current project is using Java. we push business logics to enum which represents particular state ( New, Registered and so on). In scala, I just wonder that is it a good idea to use case object to act as state ? what is the drawback.

One simple example

trait State {
   def read() : Try[String] = {
  Failure(new IllegalStateException("too young to read"))
}
}
case object child extends State

case object young extends State {
  override def read() : Try[String] = {
    Success("young people read book")
  }
}
case object elder extends State {
  override def read() : Try[String] = {
    Success("elder people read book")
  }
}

class Person(state : State) {

   def read() : Try[String] = {
      state.read()
   }
}
È stato utile?

Soluzione

It can be and in fact it is pretty common pattern (accompanied with sealed trait by the way), but implementation I've seen usually move any actions out of state and use case objects as tags that signify current state, alongside with case classes that may store some state data:

sealed trait State
case class Connected(messagesProcessed: Long) extends State
case object Connecting extends State
case object Disconnected extends State

def foo() = state match {
  case Connected(count) => "do something"; state = Connected(count + 1) // could be .copy(..)
  case Connecting       => "wait patiently"
  case Disconnected     => "trigger reconnection"
}

Reasoning behind using case classes to store data (versus storing it in plain old class fields) is that different states may have different sets of variables and with case classes it is easier to comprehend current working set.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top