1
我目前的项目正在使用Java。我们推动业务逻辑enum代表特定的状态(新建,注册等)。在scala中,我只是想知道用case对象作为状态是个好主意吗?有什么缺点。can case object as a state
一个简单的例子
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()
}
}
将您的案例类设为大写字母,例如'Child'而不是'child'是个好习惯。 –
查看Akka的[有限状态机](http://doc.akka.io/docs/akka/2.3.1/scala/fsm.html)(FSM)获取灵感。 –