状态模式(STATE)
Java深入到一定程度,就不可避免的碰到设计模式这一概念,了解设计模式,将使自己对java中的接口或抽象类应用有更深的理解.设计模式在java的中型系统中应用广泛,遵循一定的编程模式,才能使自己的代码便于理解,易于交流,State(状态)模式是比较常用的一个模式.
应用场景:如果需要在 1、不同的状态(可能会对应相应的行为), 2、不同的行为; 间反复进行切换,则应优先考虑状态模式。
在此写了7个java类来描述说明State设计模式;
1、State.java 状态接口
2、StateA.java 状态A
3、StateB.java 状态B
4、StateC.java 状态C
5、StateD.java 状态D
6、StateManager.java 状态管理类
7、StateTest.java 带有main方法的测试类
=============== 1、State.java 状态接口
public interface State {
//执行'上一步' 操作
public void lastStep(StateManager c);
//执行'下一步' 操作
public void nextStep(StateManager c);
}
=============== 1 end
=============== 2、StateA.java 状态A
public class StateA implements State {
//A->null(end)
public void lastStep(StateManager c) {
// TODO Auto-generated method stub
c.setState(null);
}
//A->B
public void nextStep(StateManager c) {
// TODO Auto-generated method stub
c.setState(new StateB());
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "State A";
}
}
=============== 2 end
=============== 3、StateB.java 状态B
public class StateB implements State {
//B->A
public void lastStep(StateManager c) {
// TODO Auto-generated method stub
c.setState(new StateA());
}
//B->C
public void nextStep(StateManager c) {
// TODO Auto-generated method stub
c.setState(new StateC());
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "State B";
}
}
=============== 3 end
=============== 4、StateC.java 状态C
public class StateC implements State {
//C->B
public void lastStep(StateManager c) {
// TODO Auto-generated method stub
c.setState( new StateB() );
}
//C->D
public void nextStep(StateManager c) {
// TODO Auto-generated method stub
c.setState(new StateD());
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "State C";
}
}
=============== 4 end
=============== 5、StateD.java 状态D
public class StateD implements State {
//D->C
public void lastStep(StateManager c) {
// TODO Auto-generated method stub
c.setState(new StateC());
}
//D->nlll(end)
public void nextStep(StateManager c) {
// TODO Auto-generated method stub
c.setState(null);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "State D";
}
}
=============== 5 end
=============== 6、StateManager.java 状态管理类
public class StateManager {
//状态
private State state;
//没有状态的构造器(不可见)
private StateManager(){}
//带有初始状态的构造器
public StateManager(State initState){
this.state = initState;
}
//设置初始状态
public void setState(State state) {
this.state = state;
}
//下一步
public void nextStep(){
if(this.state != null)
this.state.nextStep(this);
}
//上一步
public void lastStep(){
if(this.state != null)
this.state.lastStep(this);
}
//返回下一个状态
public State nextState(){
return this.state;
}
}
=============== 6 end
=============== 7、StateTest.java 状态测试类
public class StateTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//顺行起点A
State state = new StateA();
StateManager controller = new StateManager(state);
for (int i = 0; i < 6; i++) {
state = controller.nextState();
System.out.print(state);
if(state == null) break;
System.out.print("->");
//顺流
controller.nextStep();
}
System.out.println("");
//顺行起点D
State converseState = new StateD();
controller = new StateManager(converseState);
for (int i = 0; i < 6; i++) {
state = controller.nextState();
System.out.print(state);
if(state == null) break;
System.out.print("->");
//逆流
controller.lastStep();
}
}
}
=============== 7 end
console:
State A->State B->State C->State D->null
State D->null