Design patterns in the software domain provide a useful way for developers to use expert design experience. The design pattern uses the Object-oriented programming language the important characteristic: the encapsulation, the inheritance, the polymorphism, the true understanding design pattern essence is possibly a long process, needs the massive practical experience accumulation. Recently read the design pattern of the book, for each model, with C + + wrote a small example, deepen understanding. The main reference "Dahua design pattern" and "design pattern: Reusable object-oriented Software Foundation" two books. This article describes the implementation of the state pattern.
State mode: Allows an object to change its behavior when its internal state changes. The object appears to modify its class. It has two uses: (1) The behavior of an object depends on its state, and it must change its behavior according to state at run time. (2) An operation contains large, multiple-branched conditional statements that depend on the state of the object. The example of this article is the first case, taking war as an example, assuming that a war takes four stages: Prophase, metaphase, late, and end. When the war is at different stages, the behavior of the war is not the same, it is said that the behavior of war depends on the stage, and with the advance of time is dynamic change. The corresponding UML diagram is given below.
The implementation of the code is relatively simple, given the war class and State class, the war class contains the state object (pointer form).
Class War;
Class state
{public
:
virtual void prophase () {}
virtual void metaphase () {}
virtual void anaphase ( {}
virtual void end () {}
virtual void CurrentState (War *war) {}
};
Warfare
class War
{
private: State
*m_state; Current state
int m_days; War duration public
: Wars
(State *state): M_state (state), M_days (0) {}
~war () {delete m_state;}
int GetDays () {return m_days;}
void setdays (int days) {m_days = days;}
void SetState (state *state) {delete m_state; m_state = State;}
void GetState () {m_state->currentstate (this);}
};
Give a specific status class:
War over class Endstate:public state {public:void End (War *war)//Ending phase specific behavior {cout<< "war over" <<endl;
} void CurrentState (War *war) {End (war);};
Late class Anaphasestate:public State {public:void anaphase (War *war)//later specific behavior {if (War->getdays () < 30)
cout<< "<<war->getdays" () << "Day: The end of the war, the two sides desperate one" <<endl;
else {war->setstate (new endstate ());
War->getstate ();
} void CurrentState (War *war) {anaphase (War);}};
Medium class Metaphasestate:public State {public:void metaphase (War *war)//medium-term specific behavior {if (War->getdays () < 20)
cout<< "<<war->getdays" () << "Day: middle of war, enter stalemate stage, double hair each have loss" <<endl;
else {war->setstate (new anaphasestate ());
War->getstate ();
} void CurrentState (War *war) {metaphase (War);}};
Previous class Prophasestate:public State {public:void prophase (War *war)//pre-specific behavior {if (War->getdays () < 10) cout<< "First" <<war->getdays () ≪< "Day: The beginning of the war, both of you come to me and test each other" <<endl;
else {war->setstate (new metaphasestate ());
War->getstate (); } void CurrentState (War *war) {prophase (War);}};
How to use:
Test Case
int main ()
{
War *war = new War (new Prophasestate ());
for (int i = 1; i < 40;i + + 5)
{
war->setdays (i);
War->getstate ();
}
Delete war;
return 0;
}
I enjoy the copyright of the blog article, reproduced please indicate the source http://blog.csdn.net/wuzhekai1985