The use of State is that an object is allowed to change its behavior when its internal state is changed;
State Simulates the relevant behavior interface of Context, for the specific state, using the virtual function mechanism to map to the corresponding behavior, avoid a large number of conditional statements, making the code clearer, and easy to maintain; of course, this will bring a lot of subclasses. Maintaining these classes is also cost;
Here I have made a simple C implementation, the open shutdown state of the simulator, and the behavior in the relevant state; the changes in the Context state I have dealt with the commissioned State change, imitation book case status class uses static generation, so that the status object Become a lightweight behavior object that can be shared;
The following complete source code example, limited level, no understanding is incorrect;
#pragma Warning (Disable: 4530)
#pragma Warning (Disable: 4786)
#include
#include
Using namespace std;
Class Door_State;
Class Door_Closed;
Class Door_opened;
Class Door
{
String _name;
Door_State * _State;
Friend class door_state;
PUBLIC:
Door (char * name);
String & name ();
Void open ();
Void close ();
Void pass ();
Void report_state ();
Void change_state (Door_State * new_state);
}
Class Door_State
{
PUBLIC:
Virtual void open (door *) = 0; // Simulate the Door and status related behavior
Virtual void close (DOOR *) = 0;
Virtual void pass (door *) = 0;
Virtual void report_state (door *) = 0;
}
Class door_opened: public door_state
{
PUBLIC:
Static Door_State * Instance () // Generate a global instance, this is a lightweight behavior object, you can share
{
Static Door_opened g_obj;
RETURN & G_OBJ;
}
Virtual Void Open (Door * P);
Virtual Void Close (DOOR * P);
Virtual void pass (door *);
Virtual void report_state (DOOR * P);
}
Class Door_Closed: Public Door_State
{
PUBLIC:
Static Door_State * Instance ()
{
Static Door_Closed G_Obj;
RETURN & G_OBJ;
}
Virtual Void Open (Door * P);
Virtual Void Close (DOOR * P);
Virtual void pass (door *);
Virtual void report_state (DOOR * P);
}
Void Door_opened :: Open (Door * P)
{
COUT << p-> name (). c_str () << "" "" << endl;
}
Void Door_opened :: Close (DOOR * P) {
P-> Change_State (DOOR_CLOSED :: Instance ());
COUT << p-> name (). c_str () << "Close" << endl;
}
Void Door_opened :: Pass (Door * P)
{
Cout << "You can pass" << p-> name (). c_str () << ENDL;
}
Void door_opened :: report_state (DOOR * P)
{
COUT << p-> name (). c_str () << "is open" << endl;
}
Void Door_Closed :: Open (Door * P)
{
P-> Change_State (DOOR_OPENED :: Instance ());
COUT << p-> name (). c_str () << "Open" << Endl;
}
Void Door_Closed :: Close (Door * P)
{
COUT << p-> name (). c_str () << "" "" "<< endl;
}
Void Door_Closed :: Pass (Door * P)
{
Cout << "The door is closed, you can't pass" << p-> name (). c_str () << ENDL;
}
Void Door_Closed :: Report_State (DOOR * P)
{
COUT << p-> name (). c_str () << "is the" << endl;
}
Door :: door (char * name): _Name (name)
{
Change_State (DOOR_CLOSED :: Instance ());
}
String & Door :: name ()
{
Return _name;
}
void door :: open ()
{
_State-> Open (this);
}
Void door :: close ()
{
_STATE-> Close (this);
}
Void Door :: Pass ()
{
_STATE-> Pass (this);
}
Void door :: repeort_state ()
{
_State-> Report_State (this);
}
Void door :: change_state (Door_State * new_state)
{
_STATE = new_state;
}
Int main (int Argc, char * argv [])
{
Try
{
Door D1 ("Door 1 #"); // You can see their behavior changes with the status.
DOOR D2 ("Door 2 #"); / / can also see 2 objects sharing the same status object
D1.Report_State ();
D1.Open ();
D2.Open ();
D1.Close ();
D1.Close ();
D1.Report_State ();
D2.Report_State ();
D1.pass ();
D2.pass ();
}
Catch (Exception & E)
{
Cout << E.WHAT () << endl;
}
Return 0;
}