The state pattern allows an object to change its behavior when its internal status changes. The object appears to modify its class.
In object-oriented system development and design process, often encounter a situation is the demand change (requirement changing), often we do a design, realize a system prototype, our customers will have new requirements. And so we have to modify the existing design, the most common is the solution is to have designed and implemented a good class to add new methods to meet the needs of customers, so caught in the nightmare of design changes: constantly patching, the result is that the design is simply impossible to close, the compiler is always the entire system code.
The visitor pattern provides a solution that encapsulates an update (change) into a class (an access operation) and provides a receiving interface for the class to be changed.
Structure Chart:
Instance:
context.h
#ifndef _context_h_
#define _CONTEXT_H_
class state;
/**
*
**/
class context{public
: Context
();
Context (state* state);
~context ();
void Handle ();
void Operationforstatea ();
void Operationforstateb ();
Protected:
Private:
friend class state//indicates that the private field of the context class can be accessed in the State Class
, and it is important to access the Changestate
void Changestate (state* state);
Private:
state* _state;
#endif//~_context_h_
Context.cpp
#include "Context.h"
#include "State.h"
#include <iostream>
using namespace std;
Context::context () {
}
Context::context (state* state) {
this->_state = state;
}
Context::~context () {
delete _state;
}
void Context::handle () {
_state->handle (this);
}
void Context::changestate (state* state) {
///_state->changestate (this,state);
This->_state = State;
}
void Context::operationforstatea () {
cout<< "do operation in state A";
}
void Context::operationforstateb () {
cout<< "do operation in state B";
}
State.h
#ifndef _state_h_
#define _STATE_H_
class context;//predecessor Declaration
Class state{public
: State
();
Virtual ~state ();
virtual void Handle (context* con) = 0;
Protected:
void Changestate (context* con,state* st);
Private:
//bool changestate (context* con,state* st);
Class Concretestatea:public state{public
:
Concretestatea ();
Virtual ~concretestatea ();
void Handle (context* con);
Protected:
private:
};
Class Concretestateb:public state{public
:
concretestateb ();
Virtual ~concretestateb ();
void Handle (context* con);
Protected:
private:
};
#endif//~_state_h_
State.cpp
#include "State.h"
#include "Context.h"
#include <iostream>
using namespace std;
State::state () {
}
state::~state () {
}
void State::changestate (context* con,state* st) {
con- >changestate (ST);
Concretestatea::concretestatea () {
}
Concretestatea::~concretestatea () {
}
void Concretestatea::handle (context* con) {
con->operationforstatea ();
cout<< ":: State change from A to B" <<endl;
State::changestate (Con,new concretestateb ());
}
Concretestateb::concretestateb () {
}
Concretestateb::~concretestateb () {
}
void Concretestateb::handle (context* con) {
con->operationforstateb ();
cout<< ":: State change from B to A" <<endl;
State::changestate (Con,new Concretestatea ());
}
Main.cpp
#include "Context.h"
#include "State.h"
#include <iostream>
using namespace std;
int main (int argc,char* argv[]) {
state* st = new Concretestatea ();
context* con = new context (ST);
Con->handle ();
Con->handle ();
Con->handle ();
if (Con!= NULL)
Delete con;
if (St!= null)
st = null;
return 0;
}
You can see that in the test program, three calls to Con->handle (), because of the different con state, you can get the following output:
Do operation in the state A:: The state change from a to B does operation in the State B:: The state change from
B to a do
opera tion in State A:: State change from A to B
Applicability
The behavior of an object depends on its state, and it must change its behavior according to the state at run time.
An operation contains large, multiple-branched conditional statements that depend on the state of the object. This state is usually represented by one or more enumerated constants. Typically, there are multiple operations that contain this same conditional structure. S t a T e mode places each conditional branch into a separate class. This allows you to use the object's state as an object based on the object itself, which can be changed independently of other objects.