状态模式(State)允许一个对象在其内部状态改变的时候改变行为。结构图为:
汽车有不同的档速,当行车时,经常需要换档,换档是从低到高,换档的过程就是状态的改变过程。
实现代码:
最后输出:
当前档位:空档
挂新档,当前档位:低档
挂新档,当前档位:中档
挂新档,当前档位:高档
挂新档,当前档位:空档
汽车有不同的档速,当行车时,经常需要换档,换档是从低到高,换档的过程就是状态的改变过程。
实现代码:
//Car.h
class State;
class Car
{
public:
Car(State*);
virtual ~Car();
void SetState(State*);
void Pull();
private:
State* m_pState;
};
#include "stdafx.h"
#include "Car.h"
#include "State.h"
#include <iostream>
using namespace std;
Car::Car(State* pState)
{
m_pState = pState;
cout << *m_pState;
}
Car::~Car()
{
}
void Car::SetState(State* pState)
{
m_pState = pState;
cout << "挂新档," << *m_pState;
}
void Car::Pull()
{
m_pState->ChangeState(this);
}
//State.h
#include <iostream>
#include "Car.h"
class State
{
public:
State(char* pName);
virtual ~State();
virtual void ChangeState(Car*) = 0;
friend std::ostream& operator<<(std::ostream& os, State& state);
private:
char* m_pName;
};
//State.cpp
#include "stdafx.h"
#include "State.h"
using namespace std;
State::State(char* pName)
{
m_pName = pName;
}
State::~State()
{
}
ostream& operator<<(ostream& os, State& state)
{
return os << "当前档位:" << state.m_pName << endl;
}
//Off.h
#include "State.h"
class Off : public State
{
public:
Off();
virtual ~Off();
void ChangeState(Car*);
};
//Off.cpp
#include "stdafx.h"
#include "Off.h"
#include "Low.h"
#include <iostream>
using namespace std;
Off::Off() : State("空档")
{
}
Off::~Off()
{
}
void Off::ChangeState(Car* pCar)
{
pCar->SetState(new Low);
}
//Low.h
#include "State.h"
class Low : public State
{
public:
Low();
virtual ~Low();
void ChangeState(Car*);
};
//Low.cpp
#include "stdafx.h"
#include "Low.h"
#include "Medium.h"
Low::Low() : State("低档")
{
}
Low::~Low()
{
}
void Low::ChangeState(Car* pCar)
{
pCar->SetState(new Medium);
}
//Medium.h
#include "State.h"
class Medium : public State
{
public:
Medium();
virtual ~Medium();
void ChangeState(Car*);
};
//Medium.cpp
#include "stdafx.h"
#include "Medium.h"
#include "High.h"
Medium::Medium() : State("中档")
{
}
Medium::~Medium()
{
}
void Medium::ChangeState(Car* pCar)
{
pCar->SetState(new High);
}
//High.h
#include "State.h"
class High : public State
{
public:
High();
virtual ~High();
void ChangeState(Car*);
};
//High.cpp
#include "stdafx.h"
#include "High.h"
#include "Off.h"
High::High() : State("高档")
{
}
High::~High()
{
}
void High::ChangeState(Car* pCar)
{
pCar->SetState(new Off);
}
//main.cpp
#include "stdafx.h"
#include "Car.h"
#include "Off.h"
int main(int argc, char* argv[])
{
State* pState = new Off;
Car* pCar = new Car(pState);
pCar->Pull();
pCar->Pull();
pCar->Pull();
pCar->Pull();
return 0;
}
class State;
class Car
{
public:
Car(State*);
virtual ~Car();
void SetState(State*);
void Pull();
private:
State* m_pState;
};
#include "stdafx.h"
#include "Car.h"
#include "State.h"
#include <iostream>
using namespace std;
Car::Car(State* pState)
{
m_pState = pState;
cout << *m_pState;
}
Car::~Car()
{
}
void Car::SetState(State* pState)
{
m_pState = pState;
cout << "挂新档," << *m_pState;
}
void Car::Pull()
{
m_pState->ChangeState(this);
}
//State.h
#include <iostream>
#include "Car.h"
class State
{
public:
State(char* pName);
virtual ~State();
virtual void ChangeState(Car*) = 0;
friend std::ostream& operator<<(std::ostream& os, State& state);
private:
char* m_pName;
};
//State.cpp
#include "stdafx.h"
#include "State.h"
using namespace std;
State::State(char* pName)
{
m_pName = pName;
}
State::~State()
{
}
ostream& operator<<(ostream& os, State& state)
{
return os << "当前档位:" << state.m_pName << endl;
}
//Off.h
#include "State.h"
class Off : public State
{
public:
Off();
virtual ~Off();
void ChangeState(Car*);
};
//Off.cpp
#include "stdafx.h"
#include "Off.h"
#include "Low.h"
#include <iostream>
using namespace std;
Off::Off() : State("空档")
{
}
Off::~Off()
{
}
void Off::ChangeState(Car* pCar)
{
pCar->SetState(new Low);
}
//Low.h
#include "State.h"
class Low : public State
{
public:
Low();
virtual ~Low();
void ChangeState(Car*);
};
//Low.cpp
#include "stdafx.h"
#include "Low.h"
#include "Medium.h"
Low::Low() : State("低档")
{
}
Low::~Low()
{
}
void Low::ChangeState(Car* pCar)
{
pCar->SetState(new Medium);
}
//Medium.h
#include "State.h"
class Medium : public State
{
public:
Medium();
virtual ~Medium();
void ChangeState(Car*);
};
//Medium.cpp
#include "stdafx.h"
#include "Medium.h"
#include "High.h"
Medium::Medium() : State("中档")
{
}
Medium::~Medium()
{
}
void Medium::ChangeState(Car* pCar)
{
pCar->SetState(new High);
}
//High.h
#include "State.h"
class High : public State
{
public:
High();
virtual ~High();
void ChangeState(Car*);
};
//High.cpp
#include "stdafx.h"
#include "High.h"
#include "Off.h"
High::High() : State("高档")
{
}
High::~High()
{
}
void High::ChangeState(Car* pCar)
{
pCar->SetState(new Off);
}
//main.cpp
#include "stdafx.h"
#include "Car.h"
#include "Off.h"
int main(int argc, char* argv[])
{
State* pState = new Off;
Car* pCar = new Car(pState);
pCar->Pull();
pCar->Pull();
pCar->Pull();
pCar->Pull();
return 0;
}
最后输出:
当前档位:空档
挂新档,当前档位:低档
挂新档,当前档位:中档
挂新档,当前档位:高档
挂新档,当前档位:空档