Status mode of C ++ design mode (1)
Let's look at an example before explaining the state mode. A bank withdrawal problem: if the account balance is greater than 0, normal withdrawals are made. If the balance is between-2000 and 0, overdraft withdrawals are made. If the balance is less than-2000, the account is frozen, you cannot withdraw money.
The implementation code is as follows:
// Bank Account class Account {private: // balance int m_nBalance; public: // void WithDraw () {if (m_nBalance> 0) {cout <"normal withdrawal status" <endl;} else if (m_nBalance>-2000) {cout <"overdraft status" <endl ;} else {cout <"frozen state" <endl ;}}};The withdrawal operation has three statuses: normal status, overdraft status, and freeze status. In different states, the withdrawal operation may have different behaviors. If you need to add a new state, such as: the account balance is less than-10000, directly cancel this account, and accept the court summons, You have to modify the above Code. To execute an operation, you need to determine the state in which the operation is executed, whether the method exists in this state, and how to implement the method in a specific State, will lead to a large number of if... else condition judgment, new status, source code must be modified, against the "open and closed principle ".
Therefore, it is necessary to encapsulate these states to encapsulate the state behavior into a specific State. To solve these problems, we can use the state mode. In the state mode, we encapsulate the behavior and state transfer statements of objects in each state in one state class, these state classes are used to distribute lengthy conditional transfer statements, giving the system better flexibility and scalability.
1. Status mode Overview
Status ModeIt is used to resolve the state transition of complex objects in the system and encapsulation of downlink behavior of different States.. When an object in the system has multiple states, these States can be converted, and the state mode can be used for different states of the object. The State mode separates the state of an object from the object and encapsulates it into a special State class, so that the state of the object can be flexibly changed. For the client, you do not need to care about the object state transition and the current state of the object. The client can process the object in any State.
State Pattern: allows an object to change its behavior when its internal State changes. The object seems to have modified its class. Its alias is the State object (Objects for States). The State mode is an object behavior mode. |
Abstract state classes and specific state classes are introduced in the state mode. They are the core of the state mode.
Status mode structure
The status mode structure includes the following roles:
Context (Environment ):The environment class is also called the context class. It is an object with multiple States. Because of the diversity of Environment states and the behavior of objects in different States is different, the state is independent to form a separate state class. Maintain an abstract State class State instance in the Environment class. This instance defines the current State. In actual implementation, it is an object of the State subclass.
State (abstract State class ):It is used to define an interface to encapsulate a behavior related to a specific State of the Environment class, and declare methods corresponding to different States in the abstract state class, and implement these methods in its subclass, because the behavior of objects in different States may be different, the implementation of methods in different subclasses may be different. The same methods can be written in abstract state classes.
ConcreteState (specific state class ):It is a subclass of the abstract state class. each subclass implements a state-related Behavior of the Environment class, and each specific state class corresponds to a specific state of the environment, different States have different behaviors.
In the state mode, we encapsulate the behavior of objects in different States into different state classes. In order to make the system more flexible and scalable, at the same time, we need to abstract the common behaviors under various states and introduce abstract state roles. The typical code is as follows:
Class State {public: // declare abstract business methods. Different specific State classes can implement void handle ();};
The service methods declared in the abstract state class are implemented in the subclass of the abstract state class, that is, the specific state classes can provide completely different implementation methods, in actual use, a status class may contain multiple business methods. If the implementations of some business methods in a specific status class are identical, you can move these methods to the abstract status class, code reuse. The typical status code is as follows:
Class ConcreteState: public State {public: void handle () {// method implementation code }}The environment class maintains a reference to the abstract state class. The setState () method can be used to inject different State objects to the Environment class, and then call the State object method in the Environment class business methods, the typical code is as follows.
Class Context {private: // maintain a reference State state State to the abstract state object; // other attribute values. Changes to this attribute value may cause changes to the object State int value; public: // set the status object void setState (State state State) {this. state = state;} // call the state object's business method void request () {state. handle (); // other code }}The environment class is actually a real state object. We just extract the state-related code from the Environment class and encapsulate it into a special State class, context is an association between environment Context and abstract State. Similar to the policy mode, the policy mode encapsulates specific policies, while the environment Context references a specific policy, while the state mode encapsulates specific States, the environment Context also maintains a reference to the specific state. Both are combined to achieve software reuse.