implemented state pattern

This commit is contained in:
ismayilmalik
2018-01-14 00:45:10 +04:00
parent 0580ec9c6e
commit 0627e4101c
2 changed files with 59 additions and 0 deletions

55
behavioral/state/state.go Normal file
View File

@@ -0,0 +1,55 @@
package state
import (
"fmt"
)
type State interface {
executeState(c *Context)
}
type Context struct {
StepIndex int
StepName string
Current State
}
type StartState struct{}
func (s *StartState) executeState(c *Context) {
c.StepIndex = 1
c.StepName = "start"
c.Current = &StartState{}
}
type InprogressState struct{}
func (s *InprogressState) executeState(c *Context) {
c.StepIndex = 2
c.StepName = "inprogress"
c.Current = &InprogressState{}
}
type StopState struct{}
func (s *StopState) executeState(c *Context) {
c.StepIndex = 3
c.StepName = "stop"
c.Current = &StopState{}
}
func StateDemo() {
context := &Context{}
startState := &StartState{}
startState.executeState(context)
fmt.Println("state: ", context)
inprState := &InprogressState{}
inprState.executeState(context)
fmt.Println("state: ", context)
stopState := &StopState{}
stopState.executeState(context)
fmt.Println("state: ", context)
}