最近在看《设计模式之禅》这本书,收获良多,作者不愧是工作多年的大牛,将各种设计模式讲解的非常透彻。这里备份下书中的【 状态模式 】代码:
github:
【 https://github.com/cstriker1407/design_pattern 】
状态模式比较奇怪,它把所研究的对象的行为包装在不同的状态对象里,每一个状态对象都属于一个抽象状态类的一个子类。状态模式的意图是让一个对象在其内部状态改变的时候,其行为也随之改变。
LiftStates.java类,主要用来管理各种状态:
//状态模式把所研究的对象的行为包装在不同的状态对象里,每一个状态对象都属于一个抽象状态类的一个子类。状态模式的意图是让一个对象在其内部状态改变的时候,其行为也随之改变。
public class LiftStates
{
public static interface ILiftState
{
public void open();
public void close();
public void run();
public void stop();
}
public static final ILiftState OpenState = new LiftOpen();
public static final ILiftState CloseState = new LiftClose();
public static final ILiftState RunState = new LiftRun();
public static final ILiftState StopState = new LiftStop();
/* 简单起见,使用static */
public static ILiftState currentLiftState;
/* 简单起见,使用static
* 正常状态运行
*/
public static void normal()
{
currentLiftState = OpenState;
currentLiftState.open();
currentLiftState.close();
currentLiftState.run();
currentLiftState.stop();
}
/* 简单起见,使用static
* 检测状态运行
*/
public static void test()
{
currentLiftState = OpenState;
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();
}
}
各种状态实现:
public class LiftClose implements ILiftState
{
@Override
public void open()
{
System.out.println("the door is to open");
LiftStates.currentLiftState = LiftStates.OpenState;
}
@Override
public void close()
{
System.out.println("the door is already close");
}
@Override
public void run()
{
System.out.println("the door is to run");
LiftStates.currentLiftState = LiftStates.RunState;
}
@Override
public void stop()
{
System.out.println("the door is to stop");
LiftStates.currentLiftState = LiftStates.StopState;
}
}
public class LiftOpen implements ILiftState
{
@Override
public void open()
{
System.out.println("the door is already open");
}
@Override
public void close()
{
System.out.println("the door is to close");
LiftStates.currentLiftState = LiftStates.CloseState;
}
@Override
public void run()
{
System.out.println("state err");
}
@Override
public void stop()
{
System.out.println("the door is to stop");
LiftStates.currentLiftState = LiftStates.StopState;
}
}
public class LiftRun implements ILiftState
{
@Override
public void open()
{
System.out.println("state err");
}
@Override
public void close()
{
System.out.println("state err");
}
@Override
public void run()
{
System.out.println("the door is alreay run");
}
@Override
public void stop()
{
System.out.println("the door is to stop");
LiftStates.currentLiftState = LiftStates.StopState;
}
}
public class LiftStop implements ILiftState
{
@Override
public void open()
{
System.out.println("the door is to open");
LiftStates.currentLiftState = LiftStates.OpenState;
}
@Override
public void close()
{
System.out.println("the door is to close");
LiftStates.currentLiftState = LiftStates.CloseState;
}
@Override
public void run()
{
System.out.println("the door is to run");
LiftStates.currentLiftState = LiftStates.RunState;
}
@Override
public void stop()
{
System.out.println("the door is already stop");
}
}
发表评论