Java版状态机

结构

StateMachine.java 
Transation.java  
    四个元素:当前状态, 目标状态, 事件, 行为 
Action.java
TestMachine.java

StateMachine.java

package com.guo.state;

import java.util.ArrayList;

public class StateMachine {

        //状态
    public enum STATE{
        IDEL,
        RUN,
        STOP
    }

    //事件
    public enum EVENT{
        RUN,
        STOP,
        OVER
    }

    public STATE currentState = STATE.IDEL;
    public ArrayList trans;

    //注册
    public void regist(ArrayList trans){
        this.trans = trans;
    }

    //更新
    @SuppressWarnings("unchecked")
    public  void change(EVENT event,T t){
        for(Transation tran : trans){
            if(tran.fromState == currentState && tran.event == event){
                if(t!=null) tran.action.beforeExec(t);
                tran.action.exec();
                currentState = tran.toState;
            }
        }
    }

}


Action.java

package com.guo.state;

public interface Action<T> {
    void beforeExec(T t);
    void exec();
}

class ActionRun<T> implements Action<T>{

    @Override
    public void exec() {
        System.out.println("ActionRun: exec");
    }

    @Override
    public void beforeExec(T t) {

    }

}


class ActionStop<T> implements Action<T>{

    @Override
    public void exec() {
        System.out.println("ActionStop: exec");
    }

    @Override
    public void beforeExec(T t) {

    }

}

class ActionOver<T> implements Action<T>{

    @Override
    public void exec() {
        System.out.println("ActionOver: exec");
    }

    @Override
    public void beforeExec(T t) {

    }

}

Transation.java


package com.guo.state;

import com.guo.state.StateMachine.EVENT;
import com.guo.state.StateMachine.STATE;

public class Transation {
    public STATE fromState;
    public STATE toState;
    public EVENT event;
    public Action action;

    public Transation(STATE fromState, STATE toState, EVENT event,Action action){
        this.fromState = fromState;
        this.toState = toState;
        this.event = event;
        this.action = action;   
    }
}

测试类 TestMachine.java


package com.guo.state;

import java.util.ArrayList;

import com.guo.state.StateMachine.EVENT;
import com.guo.state.StateMachine.STATE;

public class TestMachine {
    public static void main(String[] args) {

        ArrayList list = new ArrayList();

        //IDEL => RUN
        list.add(new Transation(STATE.IDEL, STATE.RUN, EVENT.RUN, new ActionRun() {
            @Override
            public void exec() {
                super.exec();

            }

            @Override
            public void beforeExec(String t) {
                super.beforeExec(t);
                System.out.println(t);

            }
        }));

        //RUN => STOP
        list.add(new Transation(STATE.RUN, STATE.STOP, EVENT.STOP, new ActionStop() {

            @Override
            public void exec() {
                super.exec();
            }

            @Override
            public void beforeExec(Integer t) {
                super.beforeExec(t);
                System.out.println(t);
            }

        }));

        //STOP => IDEL
        list.add(new Transation(STATE.STOP, STATE.IDEL, EVENT.OVER, new ActionOver() {

            @Override
            public void exec() {
                super.exec();
            }

            @Override
            public void beforeExec(Boolean t) {
                super.beforeExec(t);
                System.out.println(t);
            }

        }));
        StateMachine machine = new StateMachine();
        machine.regist(list);

        machine.change(EVENT.RUN, "run");
        machine.change(EVENT.STOP, 1);
        machine.change(EVENT.OVER, true);

    }
}

#### 待完善
action中有可能是异步任务or同步任务,把任务包装成js中Promise的形式,就可以兼容两种情况.<参考Rxjava>

然而并没有用在实际项目中.

你可能感兴趣的:(Java)