State mode
The input signal is an event; the output is the status result, and the state mode state machine is a black box.
State mode mainly highlights two words: "Change", the state of the object determines the behavior of the state, the nature of things determines the behavior of things.
An object that is particularly important to a state by modeling the state of an object through a state machine;
The core issue of State mode processing is the migration of States, because in situations where objects exist in many states,
For each business flow, the process of jumping and migrating between states is complex.
For example, a workflow, approving a file, exists new, committed, modified.
HR Department approval, boss approval, HR approval failure, boss approval failure and other status, involving multiple role interactions, involving a lot of events,
In this case, it is more appropriate to use state mode (state machine) to model.
Encapsulate each state and the corresponding implementation steps into a simple set of classes that inherit from an interface or abstract class.
Use another context to manipulate the automatic state transitions between them, automatically realizing jumps between States through event
Participants:
Context (environment, person) defines the class that the customer is interested in.
State (Moodstate): Defines an interface to encapsulate the behavior associated with a particular status of a context
Concretestate subclasses (a specific state subclass, such as angry) each subclass implements a behavior related to the state of the context.
Their collaborative relationships are:
The context delegates the status-related request to the current Concretestate object processing.
The context can pass itself as a parameter to the state object that handles the request, which allows the state object to access the context when necessary.
The context is the primary interface that the customer uses, and the client can configure a context with the state object, and once a context is configured, his customers no longer need to deal directly with the state object.
Either the context or the Concretestate subclass can determine which state is the successor to the other state, and under what conditions the state transitions.
Context:
public class Persion {
Private moodstate State;
Public Persion () {
state = new Mad (this);//start by default to be crazy
}
public void SetState (Moodstate state) {
This.state = State;
}
public void request () {
State.dosomething ();
State.changestate ();
}
}
State interface:
Public interface Moodstate {
public void dosomething ();
public void changestate ();
}
Crazy State:
public class Mad implements Moodstate {
Persion persion;
Public Mad (Persion persion) {
This.persion = persion;
}
public void Changestate () {
Persion.setstate (New Angry (persion));
}
public void dosomething () {
System.out.println ("I ' m mad!");
}
}
Angry state:
public class Angry implements Moodstate {
Persion persion;
Public Angry (Persion persion) {
This.persion = persion;
}
public void Changestate () {
Persion.setstate (New Happy (persion));
}
public void dosomething () {
System.out.println ("I ' m angry!");
}
}
Happy state:
public class Happy implements Moodstate {
Persion persion;
Public Happy (Persion 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, and the object appears to modify the class it belongs to
*
*/
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 a bit of
$objContext = new Context (new Stateb ());
$objContext->request ();
$objContext->request ();
$objContext->request ();
$objContext->request ();
$objContext->request ();
?>
State mode Java && PHP