[Android] 简单的状态机实现

package com.lehome.statemachine;

import java.util.HashMap;

public abstract class State {

    HashMap mToStates = new HashMap<>();

    @SuppressWarnings("unused")
    private String mName = "UNKNOWN";

    public State(String name) {
        mName = name;
    }

    public void moveTo(State toState, int event) {
        if (toState == null) {
            throw new IllegalArgumentException("toState cannot be null");
        }
        mToStates.put(event, toState);
    }

    public void onStart() {};
    public void onStop(int cause) {};
    public void onReset(int cause) {};
    public void onUnhandleEvent(int event) {};

    public abstract void onEnter(State fromState, int event);
    public abstract void onLeave(State toState, int event);
}


package com.lehome.statemachine;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import android.os.Handler;

public class StateMachine {

    Set mStates = new HashSet();
    private State mInitState;
    private State mCurrentState;

    Handler mHandler;

    public StateMachine() {
        mHandler = new Handler();
    }

    public StateMachine(Handler handler) {
        mHandler = handler;
    }

    public void setInitState(State initState) {
        mInitState = initState;
    }

    public void addState(State state) {
        synchronized (this) {
            mStates.add(state);
        }
    }

    public void start() {
        synchronized (this) {
            for (State state : mStates) {
                state.onStart();
            }
            mCurrentState = mInitState;
        }
    }

    public void stop(int cause) {
        synchronized (this) {
            for (State state : mStates) {
                state.onStop(cause);
            }
        }
    }

    public void reset(int cause) {
        synchronized (this) {
            for (State state : mStates) {
                state.onReset(cause);
            }
        }
    }

    public void postEvent(int event) {
        if (mHandler == null) {
            return;
        }

        final int ev = event;
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                State nextState = mCurrentState.mToStates.get(ev);
                if (nextState == null) {
                    mCurrentState.onUnhandleEvent(ev);
                    return;
                }
                mCurrentState.onLeave(nextState, ev);
                nextState.onEnter(mCurrentState, ev);
                mCurrentState = nextState;
            }
        });
    }

    public boolean canMoveTo(State toState) {
        if (toState == null) {
            return false;
        }
        synchronized (this) {
            HashMap states = mCurrentState.mToStates;
            for (Integer event : states.keySet()) {
                if (states.get(event).equals(toState)) {
                    return true;
                }
            }
            return false;
        }
    }

    public boolean canAccept(int event) {
        synchronized (this) {
            return mCurrentState.mToStates.containsKey(event);
        }
    }

    public State getCurrentState() {
        return mCurrentState;
    }
}

查看原文:http://legendmohe.net/2015/12/01/android-%e7%ae%80%e5%8d%95%e7%9a%84%e7%8a%b6%e6%80%81%e6%9c%ba%e5%ae%9e%e7%8e%b0/

你可能感兴趣的:([Android] 简单的状态机实现)