Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Default Would this be a Finite State Machine?

    Say I have this code.

    Java 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 access processTwo from processOne directly if something happened (and not in any other way) and so on so forth. Any pointers? Thanks.
    Last edited by TheRealInjustice; 04-24-2014 at 09:17 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,534
    Rep Power
    5

    Default Re: Would this be a Finite State Machine?

    A finite state machine basically goes from one state to another based on the current state. Which means there must be some change for the driver to switch states.

    In your case, it looks like the state machine is doing the same stuff from each process.

    Regards,
    Jim
    Last edited by jim829; 04-24-2014 at 09:48 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. State machine pattern in games
    By noodle_variation_187-X in forum Java Gaming
    Replies: 2
    Last Post: 04-10-2013, 04:10 PM
  2. Replies: 2
    Last Post: 02-14-2011, 02:27 PM
  3. Replies: 6
    Last Post: 02-05-2010, 04:53 PM
  4. Replies: 0
    Last Post: 06-10-2008, 07:37 AM
  5. Concurrent Hierarchical State Machine 4.3
    By levent in forum Java Software
    Replies: 0
    Last Post: 08-03-2007, 04:44 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •