[GO] [Gang of Four] Design pattern learning --State

State-State

** Table of Contents **

I think that you often have a property that manages the state like XXXStatus, and implement it likeswitch (XXXStatus) {// perform various processing by case}. I think that the purpose of this pattern is to make each state an object so that the switch statement will continue for a long time and it will be difficult for the reader to read as the number of states increases. I will. I think it's similar to the Command pattern (https://qiita.com/takumi0620/items/f7f755a4fe0d1dbfd153).

Purpose

Causes an object to change its behavior when the internal state of the object changes. In the class, this is achieved by introducing an object that represents the state without describing the change in behavior.

Component

・ Context Class with various states -State state abstract class -ConcreteState state concrete class

Implementation

We will implement a vending machine. There are the following two states and two operations as the state of the vending machine to be implemented, and the price of the product is uniformly 100 yen. Status ・ The amount less than the price of the item has been deposited. ・ The amount of money that exceeds the price of the product has been deposited.

operation ・ Make a deposit. ・ Press the product button.

State state abstract class

State.kt


package state

interface State {
    fun coin(v: VendingMachine)
    fun push(v: VendingMachine)
}

ConcreteState State concrete class

A class that represents the state in which an amount less than the price of the item has been deposited

LackState.kt


package state

class LackState: State {
    override fun coin(v: VendingMachine) {
        v.insertMoney(50)
    }

    override fun push(v: VendingMachine) {
        v.errorMessage()
    }
}

Classes where the amount of money above the price of the item is deposited

SufficientState.kt


package state

class SufficientState: State {
    override fun coin(v: VendingMachine) {
        v.warningMessage()
    }

    override fun push(v: VendingMachine) {
        v.buy()
    }

}

Context A class with various states

Abstract class

Context.kt


package state

interface Context {
    fun coin()
    fun push()
}

Vending machine concrete class

VendingMachine.kt


package state

class VendingMachine: Context {
    companion object {
        private val lack = LackState()
        private val sufficient = SufficientState()
    }

    private var state: State = lack
    private var money = 0

    override fun coin() {
        state.coin(this)
    }

    override fun push() {
        state.push(this)
    }

    fun warningMessage() {
        println("No further deposit is required.")
    }

    fun errorMessage() {
        println("The required amount has not been entered.")
    }

    fun insertMoney(m: Int) {
        println("I put in the money.")
        money += m
        if (money >= 100) {
            state = sufficient
        }
    }

    fun buy() {
        println("Please take out the drink from the outlet.")
        state = lack
    }
}

People who use

Client.kt


package state

class Client {
    init {
        val v: Context = VendingMachine()
        v.coin()
        v.push()
        v.coin()
        v.coin()
        v.push()
    }
}

Output result

[out-put]
I put in the money.
The required amount has not been entered.
I put in the money.
No further deposit is required.
Please take out the drink from the outlet.

I think it would be better to implement the Factory class, which instantiates the VendingMachine class of type Context.

Recommended Posts

[Gang of Four] Design pattern learning --State
[Gang of Four] Design pattern learning
[Gang of Four] Design pattern learning --Singleton
[Gang of Four] Design pattern learning --Visitor
[Gang of Four] Design pattern learning --Mediator
[Gang of Four] Design pattern learning --Iterator
[Gang of Four] Design pattern learning --Composite
[Gang of Four] Design pattern learning --Prototype
[Gang of Four] Design pattern learning --Memento
[Gang of Four] Design pattern learning --Interpreter
[Gang of Four] Design pattern learning --Builder
[Gang of Four] Design pattern learning --Bridge
[Gang of Four] Design pattern learning --Proxy
[Gang of Four] Design pattern learning --Strategy
[Gang of Four] Design pattern learning --Adapter
[Gang of Four] Design pattern learning --Observer
[Gang of Four] Design pattern learning --Command
[Gang of Four] Design pattern learning --Fly Weight
[Gang of Four] Design pattern learning --Abstract Factory
[Gang of Four] Design pattern learning --Factory Method
[Gang of Four] Design pattern learning --Chain of Responsibility
[Gang of Four] Design pattern learning --Template Method
Gang of Four (GoF) Patterns in Python
Learn the design pattern "State" in Python
Design Pattern #Builder
Design Pattern #Adapter
Pattern recognition learning in video Part 1 Field of Pattern Recognition
Design Pattern #Observer
Design Pattern #Facade
Design Pattern #Strategy
Design Pattern #Singleton
Design Pattern #Proxy
Design Pattern #Factory Method
Deep learning 1 Practice of deep learning
Design Pattern #Template Method
GoF design pattern is just an "introduction of abstraction layer"