Quote Encyclopedia
(derived from design pattern): When an object's internal state changes to allow changes in its behavior, the object looks like it changed its class.
The state mode mainly solves the situation when the conditional expression that controls an object state is too complex. It is possible to simplify the complex judgment logic by transferring the judgment logic of State to a series of classes representing different states. The intent of the state pattern is to allow an object to change its behavior as it changes its internal state. The schematic class diagram of the state pattern is as follows:
Related Roles
The roles involved in the status pattern are:
Environment (context) role, also context: Defines the interface that the client is interested in, and retains an instance of a specific State class. An instance of this specific State class gives the existing state of this environment object.
Abstract State role: Defines an interface that encapsulates the behavior of a particular state of an environment (context) object.
Specific state (realstate) role: Each specific state class implements the behavior of a state of the environment (context).
Concrete Implementation
Specific Code
1. State interface
Status class interface public
interface State {
//behavioral operations public
void handle ();
}
2, the State class specific behavior realization
The concrete status class implements a public
class Realstatea implements State {
//specific behavior implementation
@Override public
void handle () {
SYSTEM.OUT.PRINTLN ("state A ...");
System.out.println ("Perform a Operation ...");
}
The concrete state class implements B public
class Realstateb implements State {
//specific behavior implementation
@Override public
void handle () {
System.out.println ("State B ...");
System.out.println ("Perform B operation ...");
}
3. Environment Role Class
Environment Role class public class context
{
//internal status
private state status;
public void SetStatus (state status) {
this.status = status;
}
Behavior Public
void request () {
status.handle ();
}
}
4. Client-side test
public class Client {public
static void Main (string[] args) {
//Create Environment Context
: New contexts ();
Set the ambient state
context.setstatus (New Realstatea ());
When the state changes, the behavior also automatically changes
context.request ();
When the state changes, the behavior also automatically changes
context.setstatus (new Realstateb ());
Context.request ();
}
The code above simply implements the state mode and runs the output:
State a ...
Perform a operation ...
State B ...
Perform b operation ...
The above code state pattern wraps the behavior of the object in a different state object, and each state object belongs to a subclass of an abstract state class. The intent of the state pattern is to allow an object to change its behavior as it changes its internal state.
Applicability
1. The behavior of an object depends on its state, and it must change its behavior according to the state at run time.
2. An operation contains a large, multiple-branched conditional statement that relies on the state of the object, which is usually represented by one or more enumerated constants, usually with multiple operations that contain the same conditional structure. 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.