
[历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2013-12-17 标题设计模式学习笔记状态模式分类编程 / 设计模式 / java 标签设计模式·java·状态模式设计模式学习笔记状态模式最近在看《设计模式之禅》这本书收获良多作者不愧是工作多年的大牛将各种设计模式讲解的非常透彻。这里备份下书中的【 状态模式 】代码github【 https://github.com/cstriker1407/design_pattern 】状态模式比较奇怪它把所研究的对象的行为包装在不同的状态对象里每一个状态对象都属于一个抽象状态类的一个子类。状态模式的意图是让一个对象在其内部状态改变的时候其行为也随之改变。LiftStates.java类主要用来管理各种状态//状态模式把所研究的对象的行为包装在不同的状态对象里每一个状态对象都属于一个抽象状态类的一个子类。状态模式的意图是让一个对象在其内部状态改变的时候其行为也随之改变。publicclassLiftStates{publicstaticinterfaceILiftState{publicvoidopen();publicvoidclose();publicvoidrun();publicvoidstop();}publicstaticfinalILiftStateOpenStatenewLiftOpen();publicstaticfinalILiftStateCloseStatenewLiftClose();publicstaticfinalILiftStateRunStatenewLiftRun();publicstaticfinalILiftStateStopStatenewLiftStop();/* 简单起见使用static */publicstaticILiftStatecurrentLiftState;/* 简单起见使用static * 正常状态运行 */publicstaticvoidnormal(){currentLiftStateOpenState;currentLiftState.open();currentLiftState.close();currentLiftState.run();currentLiftState.stop();}/* 简单起见使用static * 检测状态运行 */publicstaticvoidtest(){currentLiftStateOpenState;currentLiftState.open();currentLiftState.close();currentLiftState.open();currentLiftState.close();currentLiftState.open();currentLiftState.close();currentLiftState.close();currentLiftState.run();currentLiftState.stop();currentLiftState.run();currentLiftState.stop();currentLiftState.run();currentLiftState.stop();}}各种状态实现publicclassLiftCloseimplementsILiftState{Overridepublicvoidopen(){System.out.println(the door is to open);LiftStates.currentLiftStateLiftStates.OpenState;}Overridepublicvoidclose(){System.out.println(the door is already close);}Overridepublicvoidrun(){System.out.println(the door is to run);LiftStates.currentLiftStateLiftStates.RunState;}Overridepublicvoidstop(){System.out.println(the door is to stop);LiftStates.currentLiftStateLiftStates.StopState;}}publicclassLiftOpenimplementsILiftState{Overridepublicvoidopen(){System.out.println(the door is already open);}Overridepublicvoidclose(){System.out.println(the door is to close);LiftStates.currentLiftStateLiftStates.CloseState;}Overridepublicvoidrun(){System.out.println(state err);}Overridepublicvoidstop(){System.out.println(the door is to stop);LiftStates.currentLiftStateLiftStates.StopState;}}publicclassLiftRunimplementsILiftState{Overridepublicvoidopen(){System.out.println(state err);}Overridepublicvoidclose(){System.out.println(state err);}Overridepublicvoidrun(){System.out.println(the door is alreay run);}Overridepublicvoidstop(){System.out.println(the door is to stop);LiftStates.currentLiftStateLiftStates.StopState;}}publicclassLiftStopimplementsILiftState{Overridepublicvoidopen(){System.out.println(the door is to open);LiftStates.currentLiftStateLiftStates.OpenState;}Overridepublicvoidclose(){System.out.println(the door is to close);LiftStates.currentLiftStateLiftStates.CloseState;}Overridepublicvoidrun(){System.out.println(the door is to run);LiftStates.currentLiftStateLiftStates.RunState;}Overridepublicvoidstop(){System.out.println(the door is already stop);}}