3, using the environment class to achieve the transformation of the State
When implementing state transitions in state mode, the specific state class can transform the state by invoking the SetState () method of the Environment class context, or it can unify the context of the environment to realize the transformation of the state. . At this point, adding a new state Class may require modifying the source code of other specific state classes or environment classes, or the system cannot be converted to a new state. However, for the client, there is no need to care about the state class, you can set the default state class for the Environment class, and the transition work of the State to the specific state class or environment class to complete, the specific conversion details for the client is transparent.
In the "Smart Air Conditioning state Transition" example above, we implement the State transformation through the specific State class. In addition, we can also realize the state transformation through the Environment class, the environment class as a state manager, the unification realizes the transformation operation between various states .
The following is a simple example of how to use an environment class to implement state transitions:
A developer of a software company wants to develop a screen Magnifier tool whose specific functions are described below: After the user clicks the Magnifier button, the screen zooms in one more time, then clicks the Magnifier button screen one more time, and the screen reverts to the default size after the third click of the button. |
State is abstract, normalstate is normal size, largerstate is twice times larger, Largeststate is 4 times times larger. Screen is a class of screens, that is, the Environment class Contex.
The screen class is declared as follows:
Screen class screen{private://The current state of the m_pstate;//normal size, twice times the size, 4 times times the size of state * m_pnormalstate; State * M_PLARGERSTATE; State * M_plargeststate;public:screen (); ~screen ();//Screen click event void OnClick ();//Set the status void SetState (states * pState);};
The screen class implementation code is as follows:
Constructor Screen::screen () {//create normal size, twice times size, 4 times times Size Object m_pnormalstate = new Normalstate (); m_plargerstate = new Largerstate (); m_ Plargeststate = new Largeststate ();//initial state is normal size m_pstate = M_pnormalstate;m_pstate->display ();} Fictitious function Screen::~screen () {if (null! = m_pstate) {Delete m_pstate;m_pstate = NULL;}} Set status void Screen::setstate (state * pState) {m_pstate = pState;} Screen Click event void Screen::onclick () {if (m_pstate = = m_pnormalstate) {SetState (m_plargerstate); M_pstate->display ();} else if (m_pstate = = m_plargerstate) {SetState (m_plargeststate); M_pstate->display ();} else if (m_pstate = = m_plargeststate) {SetState (m_pnormalstate); M_pstate->display ();}}
the abstract State class and the specific State class are declared as follows:
Abstract state Class state{public:virtual void Display () = 0;};/ /Normal Size status class Normalstate:public state{public:virtual void Display ();};/ /twice times the size state class Largerstate : Public state{public:virtual void Display ();};/ /four times times the size state class Largeststate : Public state{public:virtual void Display ();};
the abstract State class and the specific State class are implemented as follows:
void Normalstate::D isplay () {printf ("normal size \ n");} void Largerstate::D isplay () {printf ("twice times the size \ n");} void Largeststate::D isplay () {printf ("4 times times the size \ n");}
The test code is implemented as follows:
#include <stdio.h> #include "State.h" int main () {screen * Pscreen = new screen ();p Screen->onclick ();p screen- >onclick ();p Screen->onclick ();d elete Pscreen;pscreen = Null;return 0;}
compiled and executed with the following results:
In the preceding code, all state transitions are performed by the Environment class Screen , the Environment class acts as the state Manager role. If you need to add a new state, such as "eight times-times state Class", you need to modify the environment class, which in some way violates the "open and close principle", but has no effect on other state classes. This is also a compromise idea in software engineering.
State mode of C + + design mode (iii)