一、 电梯状态切换
1. 抽象电梯状态
public abstract class LiftState {
//定义一个环境角色 ,也就是封装状态的变化引起的功能变化
protected Context context;
public void setContext(Context context) {
this.context = context;
}
//首先电梯门开启动作
public abstract void open();
//电梯门关闭
public abstract void close();
//电梯要能上能下,运行起来
public abstract void run();
//电梯还要能停下来
public abstract void stop();
}
2. 上下文类
public class Context {
//定义出所有电梯状态
public final static OpenningState openningState = new OpenningState();
public final static ClosingState closingState = new ClosingState();
public final static RunningState runningState = new RunningState();
public final static StoppingState stoppingState = new StoppingState();
//定义一个当前电梯状态
private LiftState liftState;
public LiftState getLiftState() {
return liftState;
}
public void setLiftState(LiftState liftState) {
this.liftState = liftState;
//把当前的环境通知到各个实现类中
this.liftState.setContext(this);
}
public void open(){
this.liftState.open();
}
public void close(){
this.liftState.close();
}
public void run(){
this.liftState.run();
}
public void stop(){
this.liftState.stop();
}
}
3. 开门状态
public class OpenningState extends LiftState{
//开启当然可以关闭了,我就想测试一下电梯门开关功能
public void close(){
//状态修改
super.context.setLiftState(Context.closingState);
//动作委托为CloseState来执行
super.context.getLiftState().close();
}
//打开电梯门
public void open() {
System.out.println("电梯门开启...");
}
//门开着时电梯就运行跑,这电梯,吓人
public void run() {
//do nothing
}
//开门还不停止
public void stop() {
//do nothing
}
}
4. 关闭状态
public class ClosingState extends LiftState{
//电梯门关闭,这是关闭状态要实现的动作
public void open() {
super.context.setLiftState(Context.openningState); //置为开门状态
super.context.getLiftState().open();
}
//电梯门关闭,这是关闭状态要实现的动作
public void close() {
System.out.println("电梯门关闭");
}
//电梯门关了就运行,这是再正常不过了
public void run() {
super.context.setLiftState(Context.runningState); //设置为运行状态
super.context.getLiftState().run();
}
//电梯门关闭,我就不按楼层
public void stop() {
super.context.setLiftState(Context.stoppingState); //设置为停止状态
super.context.getLiftState().stop();
}
}
5. 运行状态
public class RunningState extends LiftState{
//运行的时候开电梯门?你疯了!电梯不会给你开的
public void open() {
//do nothing
}
//电梯门关闭?这是肯定的
public void close() {
//do nothing
}
//这是在运行状态下要实现的方法
public void run() {
System.out.println("电梯上下运行...");
}
//这绝对是合理的,只运行不停止还有谁敢坐这个电梯?!估计只有上帝了
public void stop() {
super.context.setLiftState(Context.stoppingState); //环境设置为停止状态
super.context.getLiftState().stop();
}
}
6. 停止状态
public class StoppingState extends LiftState{
//停止状态,开门那是要的
public void open() {
super.context.setLiftState(Context.openningState);
super.context.getLiftState().open();
}
public void close() {
//do nothing
}
//停止状态再运行起来,正常得很
public void run() {
super.context.setLiftState(Context.runningState);
super.context.getLiftState().run();
}
//停止状态是怎么发生的呢?当然是停止方法执行了
public void stop() {
System.out.println("电梯停止了...");
}
}
7. 场景类
public class Client {
public static void main(String[] args) {
Context context = new Context();
context.setLiftState(new ClosingState());
context.open();
context.close();
context.run();
context.stop();
}
}
二、状态模式的定义
1. 抽象环境角色
public abstract class State {
//定义一个环境角色,提供子类访问
protected Context context;
//设置环境角色
public void setContext(Context _context){
this.context = _context;
}
//行为1
public abstract void handle1();
//行为2
public abstract void handle2();
}
2. 环境角色
public class ConcreteState1 extends State{
public void handle1() {
//本状态下必须处理的逻辑
}
public void handle2() {
//设置当前状态为state2
super.context.setCurrentState(Context.STATE2);
super.context.handle2();
}
}
&
public class ConcreteState2 extends State{
public void handle1() {
//设置当前状态为state1
super.context.setCurrentState(Context.STATE1);
//过渡到state1状态,由Context实现
super.context.handle1();
}
public void handle2() {
//本状态下必须处理的逻辑
}
}
3. 具体环境角色
public class Context {
//定义状态
public final static State STATE1 = new ConcreteState1();
public final static State STATE2 = new ConcreteState2();
//当前状态
private State CurrentState;
//获得当前状态
public State getCurrentState() {
return CurrentState;
}
//设置当前状态
public void setCurrentState(State currentState) {
CurrentState = currentState;
//切换状态
this.CurrentState.setContext(this);
}
//行为委托
public void handle1(){
this.CurrentState.handle1();
}
public void handle2(){
this.CurrentState.handle2();
}
}
4. 场景类
public class Client {
public static void main(String[] args) {
//定义环境角色
Context context = new Context();
//初始化状态
context.setCurrentState(new ConcreteState1());
//行为执行
context.handle1();
context.handle2();
}
}