Skip to content

This is an example of the State Strategy pattern implemented in Go. This is based off the PHP example by Sebastian Bergmann https://github.com/sebastianbergmann/state

Notifications You must be signed in to change notification settings

JalfResi/StateStrategyInGo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

State Strategy Pattern in Go

This is an example of the State Strategy pattern implemented in Go. This is based off the PHP example by Sebastian Bergmann sebastianbergmann/state.

State Machine

The example consists of a Door object, which can be in one of three states; open, closed or locked. There four transitions between these states; open, close, lock and unlock.

Door State Machine

Implementation Benefits

The Strategy Pattern allows us to encapsulate the actions of each state transition within the State objects themselves. This is the "how" the current state accomplishes something. The State Pattern allows us to encapsulating state-dependent behaviour. This is the "what" will be accomplished. The combination of the two allow us to keep the data of an object separate from the actions that modify that data using a State Machine to ensure that actions can only be executed when the object is in certain states.

For strict State controlled actions, this combination of patterns can simplify things, especially if implementing commands via net/rpc:

func OpenDoorCommand() error {
    door := repo.Load()

    if door.Can(OpenDoorState{}) {
        err := door.Open()

        if door.IsOpen() {
            repo.Save(door)
        }
    }

    return nil
}

Door Object Heirarchy

References

For more information on the Strategy Pattern, see Strategy (315) in the Gang of Four authored Design Patterns book

For more information on the State Pattern, see State (305) in the Gang of Four authored Design Patterns book

For the original PHP example that inspired this, see sebastianbergmann/state

About

This is an example of the State Strategy pattern implemented in Go. This is based off the PHP example by Sebastian Bergmann https://github.com/sebastianbergmann/state

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages