The state mode of C + + design mode

Source: Internet
Author: User

Here are two examples:

1, https://www.cnblogs.com/wanggary/archive/2011/04/21/2024117.html

2, https://www.cnblogs.com/jiese/p/3182342.html

There are a few generalizations about this design pattern:

The context class contains the abstract state *_state member variable that the context class declares as the frend (at least the Changestate function of the context Class).

State is in the maintenance of a status, here is a key question, how can state does not involve the content of the context, for example, the actual example of traffic lights, is now a red light status, and the red state has a 60-second countdown card. The display number for this 60-second countdown card belongs to the context. If I do this:

(first say the Realized function, enter the red light state, display 60 seconds countdown, Countdown end, then enter the red state)

1, the red light status in response to the timer, in the timer to write this,

Context->countdown = 60-sec; The SEC is initialized to 0, with the timer increasing accordingly.

Context->updatewindow (); Let the countdown card interface display Refresh

The above code is not a problem, the main problem is that the coupling is too large, in the state directly to modify the contents of the context. What should be written, as follows:

2, the red light status in response to the timer, in the timer to write this,

Context->updatecountdownshow (count); Updatecountdownshow is an interface provided by the context class

Summary below:

State is only responsible for maintaining the state, and the behavior under the condition should be responsible for the context, the state and behavior separation, reduce the coupling degree.

  

State mode seems to be so simple, a little need to pay attention to, is in each state, there is a maintenance of their own state of the variable, such as the above into the red light state, the countdown variable should be cleared, here can be two ways, in the Changestate function this position, 1, switch State, The next state is all objects with new, and the state variable is "clean" when the constructor is used to enter each state. Delete the previous state before switching to the next state. 2. Each state is defined in the context and is equivalent to a global variable. This state-related state variable is cleared before the previous state calls Context->changestate.

For new, delete very confident words in the first way a lot less code (the constructor in the initialization of the variables proposed as a function, the case of the appeal of the 2nd, as if there is no difference:)).

The state mode of C + + design 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.