Giter VIP home page Giter VIP logo

statemachine's Introduction

一个有限状态机的C++实现

状态机有几个特征

  • 状态(state)总数是有限的
  • 任意时刻,只处在一种状态之中
  • 某种条件下,会从一种状态转变(transition)到另一种状态

状态机能够很好的管理复杂业务。适用于有一个明确 且复杂的状态流的场景。状态机能够很好的提高代码的可维护性和可测试性。

示例

StateMachine stateMachine;

stateMachine.setOnStartCallback([] () { std::cout << "state machine started" << std::endl; });
stateMachine.setOnStopCallback([] () { std::cout << "state machine stopped" << std::endl; });

State* state1 = new State();
state1->setOnEnterCallback([] (const Event*) { std::cout << "entering state1" << std::endl; });
state1->setOnExitCallback([] (const Event*) { std::cout << "exiting state1" << std::endl; });

State* state2 = new State();
state2->setOnEnterCallback([] (const Event*) { std::cout << "entering state2" << std::endl; });
state2->setOnExitCallback([] (const Event*) { std::cout << "exiting state2" << std::endl; });

State* state3 = new State();
state3->setOnEnterCallback([] (const Event*) { std::cout << "entering state3" << std::endl; });
state3->setOnExitCallback([] (const Event*) { std::cout << "exiting state3" << std::endl; });

stateMachine.addTransition(12, state1, state2);
stateMachine.addTransition(13, state1, state3);
stateMachine.addTransition(23, state2, state3);
stateMachine.addTransition(31, state3, state1);
stateMachine.setInitialState(state1);

stateMachine.start();

stateMachine.postEvent(new Event(12));
stateMachine.postEvent(new Event(23));
stateMachine.postEvent(new Event(31));
stateMachine.postEvent(new Event(13));

statemachine's People

Contributors

coderyi avatar

Watchers

James Cloos 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.