State Pattern in kotlin

State pattern is one of the behavioral design pattern. State design pattern is used when an Object changes its behavior based on its internal state.

If we have to change behavior of an object based on its state, we can have a state variable in the Object and use if-else condition block to perform different actions based on the state.

State pattern is used to provide a systematic and lose-coupled way to achieve this through Context and State implementations.

We can see lot of real world examples for the need of state design pattern. Think of our kitchen mixer, which has a step down motor inside and a control interface. Using that knob we can increase / decrease the speed of the mixer. Based on speed state the behavior changes.

Example for State pattern

Let us take an example scenario using a mobile. With respect to alerts, a mobile can be in different states. For example, vibration and silent. Based on this alert state, behavior of the mobile changes when an alert is to be done. Which is a suitable scenario for state design pattern.

  • MobileAlertState : This interface represents the different states involved. All the states should implement this interface.
  • AlertStateContext : This class maintains the current state and is the core of the state design pattern. A client should access / run the whole setup through this class.
  • Vibration : Representation of a state implementing the abstract state object. Simliar to this we ahve Silent and Sound classes


Complete code for State pattern in Kotlin


interface MobileAlertState {
    fun alert(ctx: AlertStateContext)
}

class AlertStateContext {
    private var currentState: MobileAlertState? = null

    init {
        currentState = Vibration()
    }

    fun setState(state: MobileAlertState) {
        currentState = state
    }

    fun alert() {
        currentState!!.alert(this)
    }
}

class Vibration : MobileAlertState {

    override fun alert(ctx: AlertStateContext) {
        println("vibration...")
    }
}

class Silent : MobileAlertState {

    override fun alert(ctx: AlertStateContext) {
        println("silent...")
    }
}

class Sound : MobileAlertState {

    override fun alert(ctx: AlertStateContext) {
        println("tu..tu..tu..tu")
    }
}

fun main(args: Array<String>) {
    val stateContext = AlertStateContext()
    stateContext.alert()
    stateContext.alert()
    stateContext.setState(Silent())
    stateContext.alert()
    stateContext.alert()
    stateContext.setState(Sound())
    stateContext.alert()
}		


Output of Kotlin State design pattern

	
vibration...
vibration...
silent...
silent...
tu..tu..tu..tu

About Author

This article is written by Pavan, who is serving notice period in an MNC, Bangalore. He thought, let the articles speak rather than his image. He is also the same person who created the reporter for Protractor Jasmine

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • Reply