The state mode is one of the GOF23 modes. Like the command mode, it is also a behavior mode. The State mode is similar to the command mode. It is also an application of the "interface-implementation class" mode and a embodiment of the interface-oriented programming principle.
The State mode is an object creation mode. Its intention is to allow an object to change its behavior when its internal state changes. The object seems to have modified its class. A common example is a network connection class TCPConnection. The status of a TCPConnection object is in one of several different States: the connection has been Established (Established) and is being monitored, the connection is closed ). When a TCPConnection object receives a request from another object, it responds differently based on its own status.
For example, the result of an Open request depends on whether the connection is closed or established. The State mode describes how TCPConnection shows different behaviors in each State. The key idea of this mode is to introduce an abstract class called TCPState to indicate the connection status of the network. The TCPState class declares a public interface for various word classes that represent different operation statuses. The child classes of the TCPState implement behavior related to specific States. For example, the TCPEstablished and TCPClosed classes implement the established and closed statuses of the connection specific to the TCPConnection respectively.
For example, when a person is angry, happy, and crazy, doing the same thing may have different results, and his or her mood may change cyclically in these three states. A moodState class represents a person's mood. The mad, Happy, and Angry classes represent different moods.
Let's take a look at an example:
Copy codeThe Code is as follows:
<? Php
/**
* 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 ();
To understand the status mode, there are two key points:
1. There is usually only one method in the command mode interface. The status mode interface has one or more methods. In addition, the status mode implementation class method generally returns a value, or changes the value of the instance variable. That is to say, the State mode is generally related to the object state. Methods of implementation classes have different functions, covering methods in interfaces. The State mode is the same as the command mode. It can also be used to remove if... Else and other condition selection statements.
2. The main purpose is to reference an object as an instance variable. The main usage of the command mode is the parameter callback mode. The command interface is passed in as a method parameter. Then, callback the interface in the method body. The main usage of the state mode is to use the set attribute method as an instance variable or the constructor to pass in the instance of the specific implementation class of the state interface. Therefore, we can compare the similarities and differences between the command mode and the status mode.
The State mode and command mode are both very common. The mode with a small granularity is part of many larger modes. Basically, the state mode is very similar to the command mode. As long as developers have a clear understanding of singleton and Singleton, it is okay not to divide them into two models.