Using StateMachine with enums in Java

It is a common sense for a developer to be using state-machine for their app, however, I realized that I haven’t been using state-machine for a long time so I thought that I need to implement the State-machine in my app. So I decided to replace an android toggle-button that  switched the view of the fragment with a single click with a plain button. So what did I do with the plain button? I combined it with the following logic.

/**
 * Created by michael on 2016/11/03.
 */
public class FullCodeState {

    public enum State{

        HALFCODE{
            void proceed(FullCodeState entity) {
                entity.state = FULLCODE;
            }

        },
        FULLCODE{
            void proceed(FullCodeState entity) {
                entity.state = HALFCODE;
            }

        },;
 
        abstract void proceed(FullCodeState entity);
    }

    private State state = State.HALFCODE;

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public void proceed(){
        this.state.proceed(this);
    }

    private static void sendNotifcation(FullCodeState entity){}

}

The following code shows that  the  state is changed by fullCodeState.proceed() every time, the singleClick() is called, thus, changing the flow of the condition.

public void singleClick() {
if (fullCodeState.getState() == FullCodeState.State.HALFCODE) {
    fragmentwebcontainer =
            (FrameLayout)findViewById(R.id.fragment_web_container);
    fragmentwebcontainer.setVisibility(View.GONE);
    getSupportFragmentManager().beginTransaction()
            .hide(mWebSiteFragment).commit();
    fullCodeState.proceed();
} else {
    fragmentwebcontainer =
            (FrameLayout)findViewById(R.id.fragment_web_container);
    fragmentwebcontainer.setVisibility(View.VISIBLE);
    getSupportFragmentManager().beginTransaction()
            .show(mWebSiteFragment).commit();
    fullCodeState.proceed();
}
}

The following example shows you how to use an Enum for a state-machine in java. It is simple, yet a good refresher.


public class Entity {
    private enum State{
        OPEN{
            void prceed(Entity entity) {
                entity.state = PENDING_APPROVAL;
            }
        },
        PENDING_APPROVAL{
            void prceed(Entity entity) {
                entity.state = APPROVED;
            }
        },
        APPROVED{
            void prceed(Entity entity) {
                entity.state = CLOSED;
                sendNotifcation(entity);
            }
        },
        CLOSED{
            void prceed(Entity entity) {
                throw new RuntimeException("Entity already closed");
            }

        };
        abstract void prceed(Entity entity);
    }

    private State state = State.OPEN;
    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }
    public void proceed(){
        this.state.prceed(this);
    }
    private static void sendNotifcation(Entity entity){}
}

 // Check out the log
public class ShowingState {
    public static void main(String[] args) {
        Entity entity = new Entity();
        System.out.println(entity.getState());
        entity.proceed();
        System.out.println(entity.getState());
        entity.proceed();
        System.out.println(entity.getState());
        entity.proceed();
        System.out.println(entity.getState());
    }
}

I

 

Leave a Reply

Your email address will not be published. Required fields are marked *