[Design mode] after the status mode is improved

Source: Internet
Author: User

 

1. Brief Introduction

 
Status Mode

When an object's internal state changes, its behavior can be changed. This object seems to have changed its class.


The State mode should solve the problem: when the conditional expressions that control the state transition of an object are too complex,
Transferring the status judgment logic to a series of classes that indicate different states can simplify the complicated judgment logic.

Advantage: It simplifies the code for state conversion and behavior change of an object, and puts this part of code in the [State Class]
In the [Status class], each different [status value] may be serialized"
As a chain, this ensures that when different "discrete" [Status values] are set, there can be
There is a corresponding processing (behavior ).

Disadvantage: The [Customer class] should be referenced in the [Status class], and the [Status class] should also be used in the [Customer class ],
The two classes must be referenced by each other. The coupling degree is slightly higher.

 

 

2. code example

// State abstraction base class cstate {public: cstate (); Virtual ~ Cstate (); Virtual void show (CME * PME) = 0 ;}; cstate: cstate () {} cstate ::~ Cstate () {}// single state class csingle: Public cstate {public: csingle (); Virtual ~ Csingle (); Virtual void show (CME * PME) ;}; csingle: csingle () {} csingle ::~ Csingle () {} void csingle: Show (CME * PME) {If (null = PME) return; int Nage = PME-> getage (); // if .. else Concatenates the state into a chain so that each State has a corresponding processing. If (Nage <18) {cout <Endl <"" <Nage <"years old, you cannot find a girlfriend or be single !! "<Endl ;}else {PME-> setstate (New cwithgirl (); PME-> showmsg () ;}}// class cwithgirl with girlfriend status: public cstate {public: cwithgirl (); Virtual ~ Cwithgirl (); Virtual void show (CME * PME) ;}; cwithgirl: cwithgirl () {} cwithgirl ::~ Cwithgirl () {} void cwithgirl: Show (CME * PME) {If (null = PME) return; int Nage = PME-> getage (); if (Nage> = 18) {cout <Endl <"brother this year" <Nage <"years old, haha, you can have a girlfriend. Hahaha !! "<Endl ;}// my class CME {public: CME (); Virtual ~ CME (); void setstate (cstate * pstate); // set the status void showmsg (); // display the status information for int getage () const; // get age void setage (INT Nage); // set age protected: cstate * m_ptrstate; // status int m_nage; // age}; CME: CME (): m_ptrstate (null), m_nage (0) {} CME ::~ CME () {} void CME: setstate (cstate * pstate) // state changes are placed in the state class {m_ptrstate = pstate;} void CME: showmsg () // show function {m_ptrstate-> show (this);} int CME: getage () const {return m_nage ;} void CME: setage (INT Nage) {This-> m_nage = Nage ;}

 

 

 

 

Client

Int _ tmain (INT argc, _ tchar * argv []) {// for the customer, you only need to set the different ages of the CME class, and each age (Status) the corresponding // behavior is not implemented in the CME class, but is implemented in the state class CME me1; me1.setage (16); me1.showmsg (); cout <Endl; me1.setage (20); // 16 changes to 20, which is equivalent to me1.showmsg (); // The same showmsg method, in different states, the behavior is also cout <Endl; return 0 ;}

 

 

 

 

3. Additional instructions

There is a problem to be solved, that is, mutual reference between two classes. The key is that the first class is referenced, and then the function in the class appears. This is a problem. Therefore, there is no execution result in this article.

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.