Status state mode is one of the GOF23 modes, as well as the command pattern, which is also a behavior pattern. The state mode and the command pattern are quite similar, and the application of the "interface-Implementation Class" mode is the embodiment of the interface-oriented programming principle.
The state pattern is an object-creation pattern that is intended to allow an object to change its behavior when its internal state changes, and the object seems to modify his class. A more common example is the tcpconnection of a class representing a network connection in which the state of a Tcpconnection object is in one of several different states: The connection is already established (established), listening, and the connection is closed (closed). When a Tcpconnection object receives a request from another object, he responds differently depending on his state.
For example, the result of an open request depends on whether the connection is closed or the connection is established. The state mode describes how tcpconnection behaves differently in every situation. The key idea of this model is to introduce an abstract class called Tcpstate that represents the connection state of a network, and the Tcpstate class declares a common interface for various word classes that represent different operational states. Tcpstate subclasses implement behavior related to a particular state. For example, the tcpestablished and tcpclosed classes implement the behavior of a tcpconnection-specific connection established and the connection is closed respectively.
For example: A person has a state of being angry, happy, and crazy, and doing the same thing in these States may have different consequences, and a person's mood may change in these three states. Use a Moodstate class to represent a person's mood, and use the Mad,happy,angry class to represent a different mood.
Let's look at an example:
Copy CodeThe code is as follows:
/**
* 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
";
}
}
Class Stateb implements State
{
Public function handle ($context)
{
$context->setstate (New Statec ());
}
Public Function display ()
{
echo "State B
";
}
}
Class Statec implements State
{
Public function handle ($context)
{
$context->setstate (New Statea ());
}
Public Function display ()
{
echo "State C
";
}
}
Instantiate a bit of
$objContext = new Context (new Stateb ());
$objContext->request ();
$objContext->request ();
$objContext->request ();
$objContext->request ();
$objContext->request ();
There are 2 key points to understand the state pattern:
1. There is usually only one method in the interface of the command pattern. There are 1 or more methods in the interface of the state mode. Also, the method of the implementation class of the state pattern, the general return value, or change the value of the instance variable. In other words, the state pattern is generally related to the state of the object. The methods of implementing a class have different functions, overriding the methods in the interface. State mode, like command mode, can also be used to eliminate conditional selection statements such as If...else.
2. The main purpose is that, as an instance variable, is an object reference. The primary use of the command pattern is the parameter callback pattern. The command interface is passed in as a parameter to the method. The interface is then recalled in the method body. and the main use of the state mode is as an instance variable, through the Set property method, or the constructor of the state interface of the specific implementation class to pass in the instance. Therefore, the similarities and differences between command mode and state mode can be compared.
State mode and command mode are very common, and the granularity is smaller, which is part of many larger models. Basically, the state mode and the command pattern are very similar. As long as the developers have a clear understanding of singleton and multiple examples, even if you do not divide them into two modes is OK.
http://www.bkjia.com/PHPjc/323615.html www.bkjia.com true http://www.bkjia.com/PHPjc/323615.html techarticle Status State mode is one of the GOF23 modes, as well as the command pattern, which is also a behavior pattern. The state mode and the command pattern are quite similar, as is the "interface-Implementation class" mode ...