1 package com.shejimoshi.behavioral.State; 2 3 4 /** 5 * 功能:状态接口 6 * 时间:2016年3月8日下午9:41:17 7 * 作者:cutter_point 8 */ 9 public interface State10 {11 /**12 * 当前状态应该干嘛13 * @param w14 */15 public abstract void writeProgram(Work w);16 }
1 package com.shejimoshi.behavioral.State; 2 3 4 /** 5 * 功能:上午工作的状态 6 * 时间:2016年3月8日下午9:44:41 7 * 作者:cutter_point 8 */ 9 public class ForenoonState implements State10 {11 12 @Override13 public void writeProgram(Work w)14 {15 if(w.getHour() < 12)16 {17 System.out.println("当前时间:" + w.getHour() + "点上午工作, 精神百倍");18 }19 else20 {21 //设置下一个状态22 w.setState(new NoonState());23 //工作内容24 w.writeProgram(); //通过这个来调用当前的状态然后递归下去25 }26 }27 28 }
1 package com.shejimoshi.behavioral.State; 2 3 4 /** 5 * 功能:中午 6 * 时间:2016年3月8日下午9:52:25 7 * 作者:cutter_point 8 */ 9 public class NoonState implements State10 {11 12 @Override13 public void writeProgram(Work w)14 {15 if(w.getHour() < 13)16 {17 System.out.println("当前时间:" + w.getHour() + "点, 饿了, 午饭;犯困,午休。");18 }19 else20 {21 //设定下一个工作22 w.setState(new AfternoonState());23 //开始工作状态24 w.writeProgram();25 }26 }27 28 }
1 package com.shejimoshi.behavioral.State; 2 3 4 /** 5 * 功能:下午工作 6 * 时间:2016年3月8日下午9:52:58 7 * 作者:cutter_point 8 */ 9 public class AfternoonState implements State10 {11 12 @Override13 public void writeProgram(Work w)14 {15 if(w.getHour() < 17)16 {17 System.out.println("当前时间:" + w.getHour() + "点, 下午状态不错,加油努力");18 }19 else20 {21 w.setState(new EveningState());22 w.writeProgram();23 }24 }25 26 }
1 package com.shejimoshi.behavioral.State; 2 3 4 /** 5 * 功能:晚上加班 6 * 时间:2016年3月8日下午9:53:17 7 * 作者:cutter_point 8 */ 9 public class EveningState implements State10 {11 12 @Override13 public void writeProgram(Work w)14 {15 if(w.isFinish())16 {17 w.setState(new RestState());18 w.writeProgram();19 }20 else21 {22 if(w.getHour() < 21)23 {24 System.out.println("当前时间:" + w.getHour() + "点,加班哦, 疲累之极");25 }26 else27 {28 w.setState(new SleepingState());29 w.writeProgram();30 }31 }32 }33 34 }
1 package com.shejimoshi.behavioral.State; 2 3 4 /** 5 * 功能:睡眠状态 6 * 时间:2016年3月8日下午9:53:55 7 * 作者:cutter_point 8 */ 9 public class SleepingState implements State10 {11 12 @Override13 public void writeProgram(Work w)14 {15 System.out.println("当前时间:" + w.getHour() + "点, 不行了,睡着了。");16 }17 18 }
1 package com.shejimoshi.behavioral.State; 2 3 4 /** 5 * 功能:下班休息状态 6 * 时间:2016年3月8日下午9:54:19 7 * 作者:cutter_point 8 */ 9 public class RestState implements State10 {11 12 @Override13 public void writeProgram(Work w)14 {15 System.out.println("当前时间:" + w.getHour() + "点,下班啦,回家收衣服去啦");16 }17 18 }
1 package com.shejimoshi.behavioral.State; 2 3 4 /** 5 * 功能:工作事件 6 * 时间:2016年3月8日下午9:42:45 7 * 作者:cutter_point 8 */ 9 public class Work10 {11 /**12 * 当前状态13 */14 private State current;15 16 /**17 * 时间18 */19 private double hour;20 21 private boolean finish;22 23 public boolean isFinish()24 {25 return finish;26 }27 28 public void setFinish(boolean finish)29 {30 this.finish = finish;31 }32 33 public Work(State s)34 {35 this.current = s;36 }37 38 public double getHour()39 {40 return hour;41 }42 43 public void setHour(double hour)44 {45 this.hour = hour;46 }47 48 public void setState(State current)49 {50 this.current = current;51 }52 53 public void writeProgram()54 {55 current.writeProgram(this);56 }57 58 }
1 package com.shejimoshi.behavioral.State; 2 3 4 /** 5 * 功能:允许一个对象在其内部状态改变它的行为。对象看起来似乎修改了它的类 6 * 适用:一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为 7 * 一个操作中含有庞大的多分支的条件语句,并且这些分支依赖于该对象的状态。这个状态通常用一个或多个枚举常量表示。 8 * 通常,有多个操作包含这一相同的条件结构。State模式将每一个分支放入一个独立的类中。这使得你可以根据对象自身 9 * 的情况将对象的状态作为一个对象,这一对象,这一对象可以不依赖于其他对象而独立变化10 * 时间:2016年3月8日下午8:26:2611 * 作者:cutter_point12 */13 public class Test14 {15 public static void main(String[] args)16 {17 Work work = new Work(new ForenoonState());18 work.setHour(9);19 work.writeProgram();20 work.setHour(10);21 work.writeProgram();22 work.setHour(12);23 work.writeProgram();24 work.setHour(13);25 work.writeProgram();26 work.setHour(14);27 work.writeProgram();28 work.setHour(17);29 work.writeProgram();30 31 //下班32 //work.setFinish(true);33 work.setFinish(false);34 35 work.writeProgram();36 work.setHour(19);37 work.writeProgram();38 work.setHour(22);39 work.writeProgram();40 41 42 }43 }
测试结果:
当前时间:9.0点上午工作, 精神百倍当前时间:10.0点上午工作, 精神百倍当前时间:12.0点, 饿了, 午饭;犯困,午休。当前时间:13.0点, 下午状态不错,加油努力当前时间:14.0点, 下午状态不错,加油努力当前时间:17.0点,加班哦, 疲累之极当前时间:17.0点,加班哦, 疲累之极当前时间:19.0点,加班哦, 疲累之极当前时间:22.0点, 不行了,睡着了。
原标题:【设计模式】20、状态模式
关键词:设计模式