State mode of C + + design mode (i)

Source: Internet
Author: User

Let's look at an example before explaining the state pattern. A bank withdrawal question: If the account balance is greater than 0, the withdrawal is normal, and if the balance is between 2000 and 0, the overdraft is withdrawn, and if the balance is less than 2000, the account is frozen and cannot be withdrawn.

The implementation code is as follows:

Bank account class account{private://balance int m_nbalance;public://withdrawal operation 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 operation of withdrawal, there are three states of normal state, overdraft status, frozen state. In different states, the withdrawal operation should have different behavior. If you need to add a new state, such as: account balance of less than 10000, the direct cancellation of this account, and accept the court summons, you have to modify the above code. To perform an operation, it is necessary to determine in which state the operation is performed, to determine whether the method is in that state, and how to implement the method in a particular state, which will result in a large number of if...else condition judgments, new State, modification of source code, and violation of the "open closure principle".

It is therefore necessary to encapsulate these states and encapsulate the behavior of the state into a specific state. In order to solve these problems, we can use the state mode, in the state mode, we encapsulate the behavior and state transition statements of the object in each State class, through these state classes to disperse the lengthy conditional transfer statements, so that the system has better flexibility and scalability.

1. Status Mode overview

    state mode . State mode can be used when an object in the system has more than one state, which can be converted between states, and the object behaves differently in different states. State mode separates the state of an object from the object, encapsulates it into a specialized state class, makes the state of the object flexible, and for the client there is no need to care about the transformation of the State of the object and the current state of the object, regardless of the state of the object, which the client can handle uniformly.

State pattern : allows an object to change its behavior when its internal state changes, and the object seems to modify its class. Its alias is a state object (Objects for States), and the state mode is an object-behavioral pattern.

Abstract state classes and specific state classes are introduced in the state mode, which are the core of the state pattern.

State mode structure diagram

The status mode structure diagram contains the following roles:

Context (Environment Class): an Environment class, also known as a contextual class, is an object that has multiple states. Because of the diversity of the state of the environment class and the behavior of the objects in different states, the state is separated out to form a separate state class. Maintains an instance of an abstract state class in the environment class that defines the current state, which, when implemented, is an object of the status subclass.

State (Abstract status Class): it is used to define an interface to encapsulate the behavior associated with a particular state of the environment class, to declare a variety of different states in the abstract State class, and to implement these methods in their subclasses, because the behavior of the objects may be different in different states, Therefore, the implementation of the methods in different subclasses may be different, the same method can be written in the abstract state class.

Concretestate (Specific State Class): It is a subclass of the abstract state class, each of which implements a state-related behavior of the environment class, each of which corresponds to a specific state of the environment, and the behavior of different state classes differs.

In state mode, we encapsulate the behavior of objects in different state classes, in order to make the system more flexible and extensible, while encapsulating the common behavior in each state, we need to abstract the state, introduce the abstract State class role, the typical code is as follows:

Class State{public:    //Declare abstract business methods, different specific state classes can be different implementations of    void handle ();};

In the subclass of the abstract state class, which implements the business method declared in the abstract state class, different state classes can provide a completely different method implementation, in the actual use, in a state class may contain multiple business methods, if the implementation of certain business methods in the specific State class is identical, These methods can be moved to the abstract State class, to implement code reuse, the typical state-specific class code is as follows

Class Concretestate:public state{public:    void handle ()    {//method specific implementation code    }}
The Environment class maintains a reference to the abstract state class, which can inject different state objects into the environment class through the setState () method, and then invoke the method of the state object in the business method of the Environment class, as shown in the typical code below.
The class context{private://maintains a reference to an abstract state object    <span style= "White-space:pre" ></span>state states;//other property values, The change in the property value may cause the object state to change int value; Public:    //Set Status object void SetState (state) {    this.state = states;} Call the business method of the State object void request () {    state.handle ();     <span style= "White-space:pre" ></span>    //Other Code}}
The Environment class is actually the object of the real state, and we just extract the state-related code from the Environment class and encapsulate it into a specialized state class, the Environment Class context and the abstract state class is an association relationship. Similar to the policy pattern, the policy pattern encapsulates a specific strategy, the Environment Class context refers to a specific strategy, while the state pattern encapsulates a specific state, the environment class The context also maintains a reference to a specific state. Both are combined to achieve the function of software reuse.

State mode of C + + design mode (i)

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.