package stateMode; /* * 加班状态类 普通模式实现 * * * 这就是活生生的面向过程啊 那要java何用!! 让面向对象怎么看。。。 */ public class Commonly { public static void main(String[] args) { hour = 9; Work(); hour = 10; Work(); hour = 12; Work(); hour = 13; Work(); hour = 14; Work(); //完成工作 hour = 17; isFinished = true; Work(); hour = 19; Work(); hour = 22; Work(); //没完成工作 hour = 17; isFinished = false; Work(); hour = 19; Work(); hour = 22; Work(); } //标记时间点 static int hour = 0; //标记工作是否完成 static boolean isFinished = false; /* * 工作方法 */ public static void Work(){ if(hour < 12){ System.out.println("当前时间"+hour+"点 上午工作,精神百倍"); }else if(hour < 13){ System.out.println("当前时间"+hour+"点 饿了 午饭 犯困 午休"); }else if(hour < 17){ System.out.println("当前时间"+hour+"点 下午状态还行"); }else { if(isFinished){ System.out.println("当前时间"+hour+"点 下班回家"); }else{ if(hour < 21){ System.out.println("当前时间"+hour+"点 加班 加班 疲惫不堪"); }else{ System.out.println("当前时间"+hour+"点 不行了 要死了"); } } } } }
package stateMode; /* * 工作状态 这次最起码来一个面向对象啊 !! */ public class Commonly2 { //时间 private int hour; //是否完成工作 private boolean isFinished; /* * 方法太长 不是很么好事 * * 违背了单一职责原则 */ public void Work(){ if(hour < 12){ System.out.println("当前时间"+hour+"点 上午工作,精神百倍"); }else if(hour < 13){ System.out.println("当前时间"+hour+"点 饿了 午饭 犯困 午休"); }else if(hour < 17){ System.out.println("当前时间"+hour+"点 下午状态还行"); }else { if(isFinished){ System.out.println("当前时间"+hour+"点 下班回家"); }else{ if(hour < 21){ System.out.println("当前时间"+hour+"点 加班 加班 疲惫不堪"); }else{ System.out.println("当前时间"+hour+"点 不行了 要死了"); } } } } public int getHour() { return hour; } public void setHour(int hour) { this.hour = hour; } public boolean isFinished() { return isFinished; } public void setFinished(boolean isFinished) { this.isFinished = isFinished; } public static void main(String[] args) { Commonly2 commonly2 = new Commonly2(); commonly2.setHour(9); commonly2.Work(); commonly2.setHour(10); commonly2.Work(); commonly2.setHour(12); commonly2.Work(); commonly2.setHour(13); commonly2.Work(); commonly2.setHour(14); commonly2.Work(); //工作完成 commonly2.setFinished(true); commonly2.setHour(17); commonly2.Work(); commonly2.setHour(19); commonly2.Work(); commonly2.setHour(22); commonly2.Work(); //工作没完成 commonly2.setFinished(false); commonly2.setHour(17); commonly2.Work(); commonly2.setHour(19); commonly2.Work(); commonly2.setHour(22); commonly2.Work(); } }
package stateMode; /* * 状态模式 : * 当一个对象的内在状态改变时 允许改变其行为 这个对象看起来像是改变了其类 * * 状态模式主要解决的是 当控制一个对象状态转换的条件表达式过于复杂时的情况,把状态的判断逻辑转移到表示不同状态的一系列类中 可以把复杂的判断逻辑简化 * */ public class StateMode { public static void main(String[] args) { Work work = new Work(); work.hour = 9; work.Working(); work.hour = 10; work.Working(); work.hour = 12; work.Working(); work.hour = 13; work.Working(); work.hour = 14; work.Working(); //工作完成 /* work.hour = 17; work.isFinished = true; work.Working(); work.hour = 19; work.Working(); work.hour = 22; work.Working();*/ //工作未完成 work.hour = 17; work.isFinished = false; work.Working(); work.hour = 19; work.Working(); work.hour = 22; work.Working(); /* * 这个时候 如果老板强制20点之前要下班的话 只需要在EveningState这个类中修改一下判断条件就行了 */ } } /* * 工作类 */ class Work{ private State current; /* * 构造方法 初始化current 默认早上开始办公 */ public Work(){ current = new morningState(); } //工作时间 public int hour; //工作状态 public boolean isFinished; /* * 设置当前状态 */ public void SetState(State state){ current = state; } public void Working(){ current.Working(this); } } /* * 状态抽象类 */ interface State{ public abstract void Working(Work work); } /* * 上午状态 */ class morningState implements State{ public void Working(Work work) { if(work.hour < 12){ System.out.println("当前时间"+work.hour+"点 上午工作,精神百倍"); }else{System.out.println("ssssssssssssssss"); work.SetState(new NoonState()); work.Working(); } } } /* * 中午状态 */ class NoonState implements State{ public void Working(Work work) { if(work.hour < 13){ System.out.println("当前时间"+work.hour+"点 饿了 午饭 犯困 午休"); }else{ work.SetState(new AfternoonState()); work.Working(); } } } /* * 下午状态 */ class AfternoonState implements State{ public void Working(Work work) { if(work.hour < 17){ System.out.println("当前时间"+work.hour+"点 下午状态还行"); }else{ System.out.println("1111111111111111111111"); work.SetState(new EveningState()); work.Working(); } } } /* * 晚上状态 */ class EveningState implements State{ public void Working(Work work) { if(work.isFinished){ work.SetState(new RestState()); work.Working(); }else{ if(work.hour < 21){ System.out.println("当前时间"+work.hour+"点 加班 加班 疲惫不堪"); }else{ work.SetState(new SleepingState()); work.Working(); } } } } /* * 下班啊 回家睡觉啊 */ class RestState implements State{ public void Working(Work work) { System.out.println("当前时间"+work.hour+"点 下班回家"); } } /* * 不行了 要猝死了 */ class SleepingState implements State{ public void Working(Work work) { System.out.println("当前时间"+work.hour+"点 不行了 要死了"); } }