State (State) Mode

  State mode, also known as an object model, state model is the behavior of the object.

  Mode when the state allows an object to change its internal state to change their behavior. This looks like the object is to change its class the same.

1. Structure

   One sentence, the state model to study the behavior of an object wrapped in a different state objects in every state of the object belongs to a subclass of the abstract class status. The intention is to make the state a model object changes in its internal state when their behavior has changed.

Structured as follows:

 

 Role involves the following:

Environment (Context) role, as is also the context: the interface definition client is interested in, and retain a particular instance of the state class. Examples of this particular class is given the state of the current state of the object in this environment.
Abstract state (State) Roles: defining an interface for a particular package environment status (Context) corresponding to the target behavior.
Specific state (ConcreteState) role: every concrete state classes implement the environment (Context) in a state corresponding behavior.

 

Source code is as follows:

Environmental roles:

public class Context {

    private State state;

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

    public State getState() {
        return state;
    }

}

 

Abstract states:

/**
 * Status Class
 * 
 * @author Administrator
 *
 */
public interface State {

    void handleState();
}

 

Specific Status A:

public class ConcreteSateA implements State {

    @Override
    public void handleState() {
        System.out.println ( "A processing request state" );
    }

}

 

Specific Status B:

public class ConcreteSateB implements State {

    @Override
    public void handleState() {
        System.out.println ( "state processing request B" );
    }

}

 

Client:

public class Client {
    public static void main(String[] args) {
        Context context = new Context();
        State state = new ConcreteSateA();
        context.setState(state);

        State state2 = new ConcreteSateB();
        context.setState(state2);
    }
}

result:

A state of the processing request
B-state processing request

 

Note: The above change of state is determined by the client, may be determined by the particular state, that is determined by the ConcreteState its own next state.

 

2. Based on the state of the traffic light patterns to achieve

  Traffic lights junction have different state, red for stop, green for go, yellow pause. It is fixed and the status changes, the red -> green -> blue. Assuming that the red light 30s, green 30s, yellow 3s.

Structured as follows:

 Source code is as follows:

Environmental roles:

public class Context {

    private LightState state;

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

    // can be written directly to the method above, this method setState 
    void Action () {
        state.action(this);
    }

}

 

Abstract states: context transfer in order to change the state of a particular state context classes

/**
 * Status Class
 * 
 * @author Administrator
 *
 */
public interface LightState {

    void action(Context context);
}

 

Specific status categories:

public class RedLightState implements LightState {

    @Override
    public void action(Context context) {
        System.out.println ( "red light stop" );

        // simulation on for 30 seconds 
        the try {
            Thread.sleep(30 * 1000);
        } catch (InterruptedException ignored) {
            // ignore
        }

        // changed to green 
        context.setState ( new new GreenLightState ());
        context.action();
    }

}

 

public class GreenLightState implements LightState {

    @Override
    public void action(Context context) {
        System.out.println ( "green line" );

        // simulation on for 30 seconds 
        the try {
            Thread.sleep(30 * 1000);
        } catch (InterruptedException ignored) {
            // ignore
        }

        // changed to yellow 
        context.setState ( new new YellowLightState ());
        context.action();
    }

}

 

public class YellowLightState implements LightState {

    @Override
    public void action(Context context) {
        System.out.println ( "yellow pause" );

        // simulated brightness 3 seconds 
        the try {
            Thread.sleep(3 * 1000);
        } catch (InterruptedException ignored) {
            // ignore
        }

        // to red for 
        context.setState ( new new RedLightState ());
        context.action();
    }

}

 

Client:

public class Client {
    public static void main(String[] args) {
        Context context = new Context();
        context.setState(new GreenLightState());
        context.action();
    }
}

result:

Green Line
yellow pause
red light stop
green line

...

 

to sum up:

Intent: allows the object to change its behavior when the internal state is changed, the object appears to modify its class.

The main solution: an object's behavior depends on its state (property), and may be changed to change its behavior based on its associated state.

When to use: the code contains a number of conditional statements about the status of the object.

How to solve: the variety of specific state class abstracts.

The key code: only one way interfaces are typically command mode. While one or more of the interface state mode. Further, the class method implementation state mode, usually the return value, or change the value of the instance variable. In other words, the state and the state of the object model is generally related. The method of implementation class have different functions, a method of covering the interface. State mode and command mode the same can also be used to eliminate other conditions if ... else selection statement.

Application examples:  1, playing basketball athletes have normal, abnormal and extraordinary state status. 2, Bianzhong, the 'clock is the abstract interface', 'Clock A' and the like are specifically state, "Bianzhong 'specific environment (Context).

Advantages:  1, the conversion rule package. 2, enumerate the possible states, we need to determine the status of species in the state before the enumeration. 3, all with a state-related behavior into a class, and you can easily add new state, only need to change the state of an object can change the behavior of objects. 4, allows the state transition logic state of the object in one, rather than one huge conditional statement block. 5, the environment allows multiple objects share a state object, thereby reducing the number of objects in the system.

Disadvantages:  1, using the state model systems inevitably increasing the number of classes and objects. 2, the structure and implementation of the state model are more complex, if used incorrectly will result in confusion structures and code of the program. 3, the state mode support for "opening and closing principle" is not very good for the state of the mode can be switched states, adding new classes need to modify the source code status of those responsible for state transitions, or can not switch to the new state, and modify a behavior state of the class corresponding to the class also need to modify the source code.

Usage scenarios:  1, depending on the state to change behavior and change of scene. 2, condition, instead of by the switch statement.

Note: Use state mode when the behavior of constrained by the state, and the state no more than five.

 

Guess you like

Origin www.cnblogs.com/qlqwjy/p/11490220.html