State mode (State) ---- behavior notes

Copyright: Complimentary more micro-business marketing courses, please pay attention to the public No. [shop]! https://blog.csdn.net/gzw1623231307/article/details/56475418



[Mode] state (State) When the internal state of an object changes, change its behavior. It looks as if the change of the same class.



A, relates to the role
[Abstract] state (State): an interface state, a state for a particular package environment objects (Context) corresponding to the behavior.
DETAILED status] (ConcreteState): a particular state, a package implements context object (Context) state of the corresponding behavior.
[Environment roles (Context): The client defines the interface of interest and retain a particular instance of the state class. That is the current state.


Second, the advantages and disadvantages
advantages: the traffic behavior state according to a particular state is encapsulated in the class, so that the localized traffic behavior. Reducing the conditional statement. The new state can be added directly, without affecting other code.


Cons: There may be many small states category.




Third, when to use:
behavior (method) of an object, which depends on its state. This behavior must be changed with the change of state.
A conditional statement singly or multiply, and the service code and a condition longer.


[Who will determine changes in the state?]
1, if the conditions are fixed, by the Context role do.
2, if the State so that subclasses determine their own next state , and when converted, will be more flexibility.
3, may also be handed over to an external event triggers a state transition.
4, create demand class status, dynamically created.




When dealing with MM, must pay attention to her status, oh, her behavior will be different in the different states, for example, you go to the movies tonight about her, did not you interested in MM would say, "there is something the matter" you do not hate but did not like the MM would say, "Well, but you can take my colleague what?", you already like the MM would say, "What time? After watching the film how to go clubbing like? ", of course, you see a good movie during the performance, I can never hate the MM state does not like to become like oh. 


Mode Status: state model allows an object to change its behavior when its internal state changes. This looks like the object changes its class the same. State mode to study the behavior of an object wrapped in a different state objects where each object belongs to a state of 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. State model requires the creation of a subclass of a class of state for the status of each system may be achieved. When the state of the system changes, the system will change the selected subclass.


http://blog.csdn.net/xxb2008




State role

public interface State {
    void rain();
    void sun();
    void travel(Context context);
}


State 1

public class RainState implements State {
    @Override
    public void rain() {
        System.out.println("下雨ing...");
    }

    @Override
    public void sun() {
        System.out.println("下雨, 没太阳");
    }

    @Override
    public void travel(Context context) {
        rain();
        sun();
        System.out.println("下雨, 不出去, 等2秒看看");
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        context.setState(new SunState());//切换状态
    }
}

State 2

public class SunState implements State {
    @Override
    public void rain() {
        System.out.println("没雨下");
    }

    @Override
    public void sun() {
        System.out.println("好大太阳");
    }

    @Override
    public void travel(Context context) {
        rain();
        sun();
        System.out.println("太阳, 出去走走");
    }
}


Context environment role

public class Context {
    State state;

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

    public State getState() {
        return state;
    }

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


    public static void main(String[] args) {
        Context context = new Context();
        context.setState(new RainState());
        context.travel();

        System.out.println("");
        System.out.println("----------");
        context.travel();

    }
}


下雨ing...
下雨, 没太阳
下雨, 不出去, 等2秒看看

----------
没雨下
好大太阳
太阳, 出去走走




















Guess you like

Origin blog.csdn.net/gzw1623231307/article/details/56475418