Status mode of C ++ design mode (2)

Source: Internet
Author: User

Status mode of C ++ design mode (2)

2. Design and Implementation of smart Air Conditioners

A software company will develop a smart Air-Conditioning System: The system detects that the temperature is between 20---30 degrees, switches to the normal temperature state; the temperature is at 30---45 degrees, then switches to the cooling state; the temperature is less than 20 degrees, switch to the heating status. Use the status mode to design the system.

From the demand, we can see that the air conditioner can be in three states: heating, normal temperature, and cooling. There are three actions in each State: keep normal temperature, cooling, and heating.

The following code implements the abstract state of the air conditioner:

// Class AirConditionerState {public: // keep room temperature virtual void KeepNormalTemperature (AirConditioner * pAirConditioner) = 0; // refrigeration virtual void refrigerate (AirConditioner * pAirConditioner) = 0; // heating virtual void Heat (AirConditioner * pAirConditioner) = 0 ;};
The three States are declared as follows:
// Class NormalTemperatureState: public AirConditionerState {public: // void KeepNormalTemperature (AirConditioner * pAirConditioner); // refrigeration void refrigerate (AirConditioner * pAirConditioner ); // heating void Heat (AirConditioner * pAirConditioner) ;}; // refrigeration status class RefrigerateState: public AirConditionerState {public: // keep the void KeepNormalTemperature (AirConditioner * pAirConditioner ); // cooling void refrigerate (AirConditioner * pAirConditioner); // heating void Heat (AirConditioner * pAirConditioner);}; // heating status class HeatState: public AirConditionerState {public: // void KeepNormalTemperature (AirConditioner * pAirConditioner); // void refrigerate (AirConditioner * pAirConditioner); // void Heat (AirConditioner * pAirConditioner );};
In each state, there are methods for maintaining normal temperature, cooling, and heating. These methods contain an AirConditioner parameter, which is used internally to call back the temperature value of the air conditioner. Based on the temperature value, the method is used to determine how the method is implemented and how to switch to other States. The three status implementation codes are as follows:
/****************************** Normal temperature status ****** ********************************** // normal Temperature void NormalTemperatureState:: KeepNormalTemperature (AirConditioner * pAirConditioner) {int nTemperature = pAirConditioner-> GetTemperature (); if (nTemperature> 20 & nTemperature <= 30) {cout <"is already normal temperature, cannot be adjusted to normal temperature "<endl ;}// cooling void NormalTemperatureState: refrigerate (AirConditioner * pAirConditioner) {int nTemperature = pAirConditioner-> GetTemperature (); if (nTemperature> 30 & nTemperature <= 45) {pAirConditioner-> SetAirConditionerState (pAirConditioner-> GetRefrigerateState (); cout <"switch to cooling state" <endl ;}} // heating void NormalTemperatureState: Heat (AirConditioner * pAirConditioner) {int nTemperature = pAirConditioner-> GetTemperature (); if (nTemperature <= 20) {pAirConditioner-> SetAirConditionerState (pAirConditioner-> GetHeatState (); cout <"switch to heating status" <endl ;}} ******* *********************************** // keep Normal Temperature void RefrigerateState:: KeepNormalTemperature (AirConditioner * pAirConditioner) {int nTemperature = pAirConditioner-> GetTemperature (); if (nTemperature> 20 & nTemperature <= 30) {pAirConditioner-> SetAirConditionerState (pAirConditioner-> GetNormalTemperatureState (); cout <"switch to normal temperature" <endl ;}// cooling void RefrigerateState :: refrigerate (AirConditioner * pAirConditioner) {int nTemperature = pAirConditioner-> GetTemperature (); if (nTemperature> 30 & nTemperature <= 45) {cout <"is already in cooling status, cannot be adjusted to cooling status "<endl ;}// heating void RefrigerateState: Heat (AirConditioner * pAirConditioner) {int nTemperature = pAirConditioner-> GetTemperature (); if (nTemperature <= 20) {pAirConditioner-> SetAirConditionerState (pAirConditioner-> GetHeatState (); cout <"switch to heating state" <endl ;}} ******* *********************************** // keep Normal Temperature void HeatState:: KeepNormalTemperature (AirConditioner * pAirConditioner) {int nTemperature = pAirConditioner-> GetTemperature (); if (nTemperature> 20 & nTemperature <= 30) {pAirConditioner-> SetAirConditionerState (pAirConditioner-> GetNormalTemperatureState (); cout <"switch to normal temperature state" <endl ;}// cooling void HeatState :: refrigerate (AirConditioner * pAirConditioner) {int nTemperature = pAirConditioner-> GetTemperature (); if (nTemperature> 30 & nTemperature <= 45) {pAirConditioner-> SetAirConditionerState (pAirConditioner-> GetRefrigerateState (); cout <"switch to cooling state" <endl ;}// heating void HeatState :: heat (AirConditioner * pAirConditioner) {int nTemperature = pAirConditioner-> GetTemperature (); if (nTemperature <= 20) {cout <"is already heating, cannot be adjusted to heating status "<endl ;}}
The air-conditioning class, that is, the Environment class Contex, maintains a state reference and calls the State object method during implementation. The Declaration Code is as follows:
// Air conditioner class AirConditioner {private: // air conditioner name string m_strAirName; // The current temperature of the air conditioner int m_nTemperature; // AirConditionerState * temperature at normal temperature; // AirConditionerState * m_pRefrigerateState; // AirConditionerState * m_pHeatState; // AirConditionerState * m_pCurState; public: // constructor AirConditioner (string strAirName, int nTemperature); // fictitious Function ~ AirConditioner (); // adjust the temperature void SetTemperature (int nTemperature); // obtain the temperature int GetTemperature (); // set the air conditioner status void SetAirConditionerState (AirConditionerState * pAirConditionerState ); // obtain AirConditionerState * GetNormalTemperatureState (); // obtain AirConditionerState * GetRefrigerateState (); // obtain AirConditionerState * GetHeatState (); // keep the room temperature void KeepNormalTemperature (); // refrigerating void refrigerate (); // heating void Heat ();};
The air-conditioning implementation code is as follows:
// Constructor AirConditioner: AirConditioner (string strAirName, int nTemperature) {m_strAirName = strAirName; m_nTemperature = nTemperature; temperature = new temperature (); temperature = new RefrigerateState (); m_pHeatState = new HeatState (); m_pCurState = m_pNormalTemperatureState;} // fictional function AirConditioner ::~ AirConditioner () {delete temperature; temperature = NULL; delete m_pRefrigerateState; temperature = NULL; delete m_pHeatState; m_pHeatState = NULL;} // adjust the temperature void AirConditioner: SetTemperature (int nTemperature) {m_nTemperature = nTemperature;} // get the temperature int AirConditioner: GetTemperature () {return m_nTemperature;} // set the air conditioning status void AirConditioner: SetAirConditionerState (AirConditionerState * temperature) {m_pCurState = pAirConditionerState;} // obtain the AirConditionerState * AirConditioner: temperature () {return temperature;} // obtain the AirConditionerState * AirConditioner: GetRefrigerateState () {return m_pRefrigerateState;} // obtain the heating status AirConditionerState * AirConditioner: GetHeatState () {return m_pHeatState;} // keep the room temperature void AirConditioner: KeepNormalTemperature () {m_pCurState-> KeepNormalTemperature (this);} // refrigeration void AirConditioner: refrigerate () {m_pCurState-> refrigerate (this);} // heating void AirConditioner: Heat () {m_pCurState-> Heat (this );}
The test code is implemented as follows:
# Include
 
  
# Include "AirConditioner. h "using namespace std; int main () {AirConditioner * pAirConditioner = new AirConditioner (" Haier air conditioner ", 25 ); ********************* * ***/pAirConditioner-> KeepNormalTemperature (); cout <endl; ********************* * ***/pAirConditioner-> SetTemperature (33 ); pAirConditioner-> refrigerate (); cout <endl; ********************* * ***/pAirConditioner-> SetTemperature (15 ); pAirConditioner-> Heat (); ********************* * ***/delete pAirConditioner; pAirConditioner = NULL; return 0 ;}
 
Compile and execute the command. The result is as follows:


Encapsulate specific behaviors in normal temperature, cooling, and heating. The air-conditioning class (that is, the Environment class) maintains a reference of the current State. When the client calls the environment class method, the call operation is delegated to the specific state class. The specific status class implements the behavior in this status and controls the switch to another status. The client does not need to directly operate on the specific status class, but is handled by the Environment class, reducing the coupling between the client and the specific status class. If you need to add a specific status class, you only need to inherit from the abstract status class and slightly modify the environment class. In addition, It also avoids a large number of if... else bloated statements and encapsulates these condition judgments into state classes.






Related 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.