Is this a finite state machine?

Say I have this code.


public interface State {
    void processOne();
    void processTwo();
    void processThree();
}

public class AStateImplOne implements State {

    @Override
    public void processOne() {
        doStuff();
    }


    @Override
    public void processTwo() {
        doStuff();
    }


    @Override
    public void processThree() {
        doStuff();
    }
    
    private void doStuff() {}
    
}

public class AStateImplTwo implements State {


    @Override
    public void processOne() {
        doStuff();
    }


    @Override
    public void processTwo() {
        doStuff();
    }


    @Override
    public void processThree() {
        doStuff();
    }
    
    private void doStuff() {}
    
}

public class StateMachine {    
    private State one = new AStateImplOne();
    private State two = new AStateImplTwo();
    private State state;
    
    public int loop() {
        checkState();
        state.processOne();
        state.processTwo();
        state.processThree();
        return 0;
    }
    
    private void checkState() {
        if (condition) {
            state = one;
        } else {
            state = two;
        }
    }
}

I don’t think this is right - you should class processTwo from processOne directly and so on so forth. Any pointers? Thanks.

Use code tags please.

Edit :
Thanks :slight_smile:

Done.

I’m not sure of the definition of a state machine, but I know that OpenGL is a great example of a state machine though. I believe that the purpose of a state machine is so that you can set its ‘state’ and then not have to call functions with large amounts of parameters that are often duplicated in future calls to that function. Also because an API like OpenGL is a ‘State Machine’, it doesnt have to check to see if your rendering parameters changed since your last render call, which offers a performance increase.