gabrielfv
01/24/2020, 6:29 PMinterface StateMachine<S> {
fun updateState(state: S)
}
Where S
needs to be a class that is able to represent the state of an UI element. What would you set as the upper-bound of S
?
// 1
interface StateMachine<S : Any>
or
// 2
interface StateMachine<S : State>
// where
interface State // There is no specific contract expected
Czar
01/28/2020, 5:34 PMState
a sealed class instead of an interface, if possible. In fact why not make the StateMachine
itself a sealed class hierarchy and get rid of the generic parameter altogether?
E.g.
sealed class Door
class OpenDoor : Door()
class ClosedDoor : Door()
// instead of
interface DoorState
class Closed : DoorState
class Open : DoorState
class Door<DoorState>
//this would make use of the state machine more type safe, and no dealing with type erasure.
gabrielfv
01/28/2020, 5:42 PMStateMachine
is an API to be implemented in multiple different contexts (thus why it's an interface
, not a class
). So the concrete usage would be something like you mentioned:
class MyStateMachine : StateMachine<Door> {
// ...
}
sealed class Door [: State] {
class OpenDoor : Door()
}
Czar
01/28/2020, 5:59 PM