《JAVA与模式》之状态模式

在阎宏博士的《JAVA与模式》一书中开头是这样描述状态(State)模式的:

  状态模式,又称状态对象模式(Pattern of Objects for States),状态模式是对象的行为模式。

  状态模式允许一个对象在其内部状态改变的时候改变其行为。这个对象看上去就像是改变了它的类一样。


状态模式的结构

  用一句话来表述,状态模式把所研究的对象的行为包装在不同的状态对象里,每一个状态对象都属于一个抽象状态类的一个子类。状态模式的意图是让一个对象在其内部状态改变的时候,其行为也随之改变。状态模式的示意性类图如下所示:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkkAAADbCAIAAAD205ICAAAfcElEQVR4nO2dLazkyNWGO+RqwYBBUZQfaQMsXRBFoyRggcEGrDRSEmCUgStZkZZFigw2aJG1MMTRgigKKLDYIBpoELDEUhQ0KjRBvnCAwYX+wPnu0UnZ7evudtunT70P6nZX26dO/bz156rDAAAAANjisLcBAAAAwMpA2wAAAFgD2gYAAMAa0DYAAADWgLYBAACwBrQNAACANaBtAAAArAFtAwAAYA1oGwAAAGtA2wAAAFjjNG179erVAWjl1atXV8olAABwW5ymbYcD+nl6QeoAAAABbbMDUgcAAAhomx2QOgAAQEDb7IDUAQAAAtpmB6QOAAAQ0DY7IHUAAIC4YW3z3nvv97ZCEapSBwAAduRWtS3LsqIoyrJMkqSqqr3NUYGe1AEAgH25SW3Lsoz1rGmaPM/PuElRFJfY0DRN27aX3GF1lKQOAADszu1pm/c+SRJ5pa7rmfB9309eTNP0EjPyPIe2AQCATm5P28qyPCZLRVGkaZplWVmWwzB479M0zfM8yzK6TsHoyuFwyLIsyzKetKvrOnuCrjjn0jRNksQ5lyRJmqbee+dclmX0Ncsy59z1I70IDakDAAAasKNtNP1Gn/M8J8mhwNR1S9NU9rSCzl/XdXzbqqpY3qibSP082QUM7qYBDakDAAAasKNtSZKw2DRNQ2HKsmTBS9OUPw8jbSvLkoYZ27alXhr/VBRFkiTByCe0DWwG9ijfGGw7boDb07aqqgJZIo2RF9u2pa8naRsNZjL8U13XSZI0T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XIVxujNl9B2wAAAFgD2gYAAMAa0DYAAADWgLYBAACwBrQNAACANaBtAAAArAFtAwAAYA1oGwAAAGtA2wAAAFgD2gYAAMAa0DYAAADWgLYBAACwBrQNAACANaBtAAAArPF/cYqyRcVcaC4AAAAASUVORK5CYII=" alt="" />

  状态模式所涉及到的角色有:

  ●  环境(Context)角色,也成上下文:定义客户端所感兴趣的接口,并且保留一个具体状态类的实例。这个具体状态类的实例给出此环境对象的现有状态。

  ●  抽象状态(State)角色:定义一个接口,用以封装环境(Context)对象的一个特定的状态所对应的行为。

  ●  具体状态(ConcreteState)角色:每一个具体状态类都实现了环境(Context)的一个状态所对应的行为。


状态模式跟策略模式有点像,对比后单纯的从代码结构来看,会发现状态模式多了一个state,这个state是在各个state实现类中的开关

举个例子,风扇有一档,二档和三档,当你按下一档,二档和三档处于可按状态,当你按下二档,一档和三档处于可按状态。好别扭看着,不过说明了他跟策略模式的本质区别

1.策略模式是并行的,而状态模式是互斥的

2.状态模式是内部自己控制的,而策略模式是client控制的(客户端必须知道有多少中策略模式)

 public class State {

     public static void main(String[] args) {

         /*BadState badState =new BadState(1);
badState.say();*/ IState state=new BlueState();
ContextState context=new ContextState(state);
System.out.println(context.getState().getState());
context.push(); System.out.println(context.getState().getState());
} } /**
*
* 这个是简单点的情况
* 复杂点 加入有三个状态 有1 则下个状态 必然是 2 上个状态是1 ‘
* 状态是2 上一个状态是1 下个状态是 3
* 以此类推
*
* 假如有五个状态以上 估计代码就很难维护了
* */
class BadState{ private int state ; public BadState(int state){ this.state=state;
} public void say(){ switch (state) {
case 1:
System.out.println(" hello ");
break;
case 2:
System.out.println(" hi ");
break; default:
break;
} } } class ContextState {
/***
* 红
* 蓝 绿
* */ private IState state; public ContextState (IState state){
this.state =state; } public IState getState() {
return state;
} public void setState(IState state) {
this.state = state;
} public void push(){ state.next(this); }
public void pull(){ state.prev(this); } } interface IState{ public void prev(ContextState c); public void next(ContextState c); public String getState(); } /***
* 红
* 蓝 绿
* */ class BlueState implements IState{ @Override
public void prev(ContextState c) { c.setState(new RedState());
} @Override
public void next(ContextState c) {
// TODO Auto-generated method stub c.setState(new GreenState());
} @Override
public String getState() {
// TODO Auto-generated method stub
return "blue";
}
}
/***
* 红
* 蓝 绿
* */ class GreenState implements IState{ @Override
public void prev(ContextState c) { c.setState(new BlueState());
} @Override
public void next(ContextState c) {
c.setState(new RedState()); } @Override
public String getState() {
// TODO Auto-generated method stub
return "green";
}
}
/***
* 红
* 蓝 绿
* */ class RedState implements IState{ @Override
public void prev(ContextState c) { c.setState(new GreenState());
} @Override
public void next(ContextState c) {
c.setState(new BlueState()); } @Override
public String getState() {
// TODO Auto-generated method stub
return "red";
}
}

ps:通过策略模式+工厂模式 可以比较好的解决if else过多的问题  当然这时候 使用策略模式+工厂模式

也是可以的,因为有多少种情况(策略)你肯定知道的

参考:http://www.cnblogs.com/java-my-life/archive/2012/06/08/2538146.html

上一篇:iOS - 开发中调试小技巧


下一篇:lua学习笔记11:lua中的小技巧