definition: allows an object to change its behavior when its internal state changes. The object appears to modify its class.
Type: object behavior pattern
class Diagram:
The state model mainly solves the problem of different processing operation according to different states, which is often encountered in the development, and most people use switch-case statements to deal with this problem, which causes a problem: too many branches, And if you add a new state, you need to compile the original code. The state mode handles these problems in a way that encapsulates these different states, handles them when the status changes, and then switches to another state, which means that the state's switching responsibility is given to the specific State class to be responsible. Meanwhile, there are many similarities between the states mode and the strategy pattern. , it should be said that the two ideas are consistent, but the package is different: Thestate pattern encapsulates different states, while the stategy pattern encapsulates different algorithms .
Structure of State pattern
Context : A contextual environment that defines the classes that customers are interested in. State : defines an interface to encapsulate the behavior associated with a particular state of the context. concretestate: a specific state subclass in which each subclass implements a behavior associated with a state of the context.
Collaborative Relationships
The context delegates the state-related actions to the current Concretestate object processing. The context can pass itself as a parameter to the state object that handles the request. This allows the state object to access the context as necessary. Either the context or the concrete state class determines which state is the successor of the other, and under what conditions it is converted. That is to say, you can save a reference to concrete state in state, set specific states when necessary, and make transitions to states. Generally speaking, when the state transition is fixed, the state transition is suitable for the context. However, when the conversion is more dynamic, it is usually carried out in a specific state class. (The State class holds the reference to the context and implements the transformation of the state).
Implementation Code
Status Abstract interface interface state {public void dosomething ();
public void changestate ();
}//Red light status class RedState implements state {private Controller Controller;
Public RedState (Controller machine) {this.controller = machine;
@Override public void DoSomething () {System.out.println ("I am red light ...");
@Override public void Changestate () {controller.setstate (new Yellowstate (Controller));
}//Yellow light status class Yellowstate implements state {private Controller Controller;
Public yellowstate (Controller Controller) {this.controller = Controller;
@Override public void DoSomething () {System.out.println ("I AM yellow light ...");
@Override public void Changestate () {controller.setstate (new Greenstate (Controller));
}//green light status class Greenstate implements state {private Controller Controller;
Public greenstate (Controller Controller) {this.controller = Controller; @Override public void DoSomething () {System.out.println ("I am Green LigHT ... ");
@Override public void Changestate () {controller.setstate (new RedState (Controller));
}//Controller--> equivalent to context class Controller {private state State;
Public Controller () {this.state = new RedState (this);
public void SetState (state state) {this.state = state;
public void DoSomething () {state.dosomething ();
State.changestate (); }//Client calls public class Stateclient {/** * @param args */public static void main (string[] args) {controll
ER controller = new Controller ();
for (int i=0;i<10;i++) {controller.dosomething (); }
}
}
Applicable Scenarios
The behavior of an object depends on its state, and it must change its behavior according to the state at run time. An operation contains a large, multiple-branched conditional statement, and these branches depend on the state of the object, which is usually represented by one or more enumerated constants. Typically, there are multiple operations that contain this same conditional structure, and the state pattern places each conditional branch into a separate class. This allows you to use the object's state as an object based on the object itself, which can be changed independently of other objects.
advantages of State mode
Structure clear: Avoid too much switch......case or if......else statements, avoid the complexity of the program, improve the maintainability of the system. Follow the design principles: a good embodiment of the open and closed principle and a single responsibility principle, each State is a subclass, you want to increase the state will increase the subclass, you want to modify the state, as long as the modification of a subclass can be. Encapsulation is very good: this is also the basic requirements of State mode, state transformation placed into the internal implementation of the class, the external call does not know how the internal implementation and behavior of the transformation.
disadvantages of State mode
Subclasses will be too much, that is, class expansion, if a thing has a lot of state is not uncommon, if the full use of State mode there will be too many subclasses, bad management, this needs to be measured by the user.
Considerations for State mode
The state pattern is useful when an object changes in its state, which means that the state pattern can be used when the behavior is constrained by the state, and the state of the object is not more than 5 when it is being used.