Php design mode State (State mode) _ PHP Tutorial

Source: Internet
Author: User
Php design mode State (State mode ). 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 interface-implementation class mode. the state mode is one of the GOF23 modes. it is also a behavior mode like the command 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:

The code is as follows:


/**
* 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
";
}
}
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 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.

Bytes. The status mode is similar to the command mode. it is also the "interface-implementation class" mode...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.