Status Mode
An input signal is an event, an output is a State result, and a state machine is a black box.
The State mode highlights two main words: "change". The state of an object determines the state behavior, and the nature of a thing determines the behavior of a thing.
A state machine is used to model the state of an object;
The core issue of state mode processing is State migration, because the object has many states,
For each business flow, the jump and migration process between different States is complicated.
For example, if a workflow approves a file, a file is created, submitted, or modified.
The status of the HR department is under approval, the boss is under approval, the HR is rejected, and the boss is rejected. It involves multiple role interactions and many events,
In this case, it is more appropriate to use the State mode (state machine) for modeling;
Encapsulate various states and corresponding implementation steps into a group of classes that simply inherit from an interface or abstract class,
Use another context to perform automatic state changes between them, and use event to automatically redirect between States.
Participants:
Context (environment, person) defines the class that the customer is interested in.
State (moodstate): defines an interface to encapsulate a behavior related to a specific State of context.
Concretestate subclasses (a specific State subclass, such as angry) each subclass implements a behavior related to the context state.
Their collaboration relationships are:
Context delegates state-related requests to the current concretestate object for processing.
Context can pass itself as a parameter to the State object that processes the request, so that the State object can access context when necessary.
Context is the main interface used by the customer. The customer can use a State object to configure a context. Once a context is configured, the customer no longer needs to directly deal with the State object.
The context or concretestate subclass can determine which State is the successor of another State, and under which conditions the State is converted.
Context:
Public class persion {
Private moodstate state;
Public persion (){
State = new mad (this); // The default start is crazy.
}
Public void setstate (moodstate state ){
This. State = State;
}
Public void request (){
State. dosomething ();
State. changestate ();
}
}
Status interface:
Public interface moodstate {
Public void dosomething ();
Public void changestate ();
}
Crazy status:
Public class mad implements moodstate {
Persion;
Public mad (persion ){
This. persion = persion;
}
Public void changestate (){
Persion. setstate (new angry (persion ));
}
Public void dosomething (){
System. Out. println ("I'm mad! ");
}
}
Angry status:
Public class angry implements moodstate {
Persion;
Public angry (persion ){
This. persion = persion;
}
Public void changestate (){
Persion. setstate (new happy (persion ));
}
Public void dosomething (){
System. Out. println ("I'm angry! ");
}
}
Happy status:
Public class happy implements moodstate {
Persion;
Public happy (persion ){
This. persion = persion;
}
Public void changestate (){
Persion. setstate (New mad (persion ));
}
Public void dosomething (){
System. Out. println ("I'm happy! ");
}
}
/**
* Status Mode
*
* Allows an object to change its behavior when its internal state changes. The object seems to have modified its class
*
*/
Interface State
{
Public Function handle ($ State );
Public function display ();
}
Class Context
{
Private $ _ state = NULL;
Public Function _ construct ($ state)
{
$ This-> setstate ($ State );
}
Public Function setstate ($ state)
{
$ This-> _ state = $ state;
}
Public function request ()
{
$ This-> _ state-> display ();
$ This-> _ state-> handle ($ this );
}
}
Class statea implements State
{
Public Function handle ($ context)
{
$ Context-> setstate (New Stateb ());
}
Public function display ()
{
Echo "State A <br/> ";
}
}
Class Stateb implements State
{
Public Function handle ($ context)
{
$ Context-> setstate (New STATEC ());
}
Public function display ()
{
Echo "State B <br/> ";
}
}
Class STATEC implements State
{
Public Function handle ($ context)
{
$ Context-> setstate (New statea ());
}
Public function display ()
{
Echo "State C <br/> ";
}
}
// Instantiate it
$ Objcontext = new context (New Stateb ());
$ Objcontext-> request ();
$ Objcontext-> request ();
$ Objcontext-> request ();
$ Objcontext-> request ();
$ Objcontext-> request ();
?>