Implementation of state-run classes

Suppose you want to implement the "CAR" class.

This class now contains various states, such as "unlit", "ignited", "broken", "punctured", etc.

The way I am trying to implement this requirement is to have boolean flags as "properties" in the class and "check state" using those boolean flags inside each member function. For example,

CAR.goRacing () 
{
    if (bIsPunctured)
       return ENUM_CANT_DRIVE;

    // Start the Engine ... 
}

This implementation, trivial to look at, starts to get very complex as the number of states the object provides increases. I've also seen cases where state alone makes maintaining an object very cumbersome (I'm sure my programming skills are at fault in this case)
Is there a standard way to implement such a state-managed object?

I have seen Steve Yeggey Property Pattern , but I really am not answering with a real example!

Thank.

+1


source to share


2 answers


This definitely sounds like a job for the State Pattern.

Basically you create your application (context) and define behavior for some operation in a separate class / interface (state)

This class will have different implementation subclasses, one for each state.

When the state changes, the state object assigns the new state to the context. This way you have all the status code in separate classes and avoid the classic spaghetti code.

EDIT

Here is the simplest implementation I could think of.

Suppose you have a "Bird". The pate does something according to what it is in.



When this state is completed, the state will change to the following (sleep -> wake up -> play, etc.)

I don't even know if it compiles. This is just to get an idea *

//These are the states of the "Pet" class
//sleep -> wake up ->  play -> dinner -> sleep -> wake up .. etc. 
class Pet {

    State currentState;
    static Pet createNew(){
        Pet p = new Pet();
        State s = new Sleep();
        s.setContext( p );
        p.currentState = 
        return p;
    }

    public void doSomething(){ 
        currentState.doSomething();
   }

}   

abstract class State {
    Pet context;
    void setContext( Pet c ) { 
        this.context = c;
    }
    abstract doSomething();
    void sout( Striing message ) { 
        System.out.println( message );
    }
}
class Sleep extends State { 
    public void doSomething(){
       sout( "zzzZZZ ... " );
       State nextState = new WakeUp();
       nextState.setContext( context );
       context.currentState = nextState;
    }
}
class WakeUp extends State { 
    public void doSomething(){
       sout( "uuuaaaaww... zzzz What time is it? ... " );
       State nextState = new Play();
       nextState.setContext( context );
       context.currentState = nextState;
    }
}
class Play extends State { 
    public void doSomething(){
       sout( "boing, boing, poing, poing" );
       State nextState = new Dinner();
       nextState.setContext( context );
       context.currentState = nextState;
    }
}
class Dinner extends State { 
    public void doSomething(){
       sout( "Yum, yum..." );
       State nextState = new Sleep();
       nextState.setContext( context );
       context.currentState = nextState;
    }

      

}

And then your client class just uses it!

Pet myPet = Pet.createNew();

while( I_FeelLikePlaying() ){ 
    myPet.doSomething();
}

      

+3


source


See here for an improved version of using the state pattern by taking advantage of .NET objects. A state can be a simple function (which will do most of the time), but it can also be an object (or some other state machine) ...

Hello,



Andreas

0


source







All Articles