Giter Site home page Giter Site logo

jalfresi / statestrategyingo Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 83 KB

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

Go 100.00%

statestrategyingo's Introduction

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

statestrategyingo's People

Contributors

jalfresi avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.