一、例:工作状态
class Work {
private int Hour;
private boolean WorkFinished = false;
public int getHour() {
return Hour;
}
public void setHour(int hour) {
Hour = hour;
}
public boolean isWorkFinished() {
return WorkFinished;
}
public void setWorkFinished(boolean workFinished) {
WorkFinished = workFinished;
}
public void WriteProgram() {
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 (WorkFinished) {
System.out.println("当前时间:" + Hour + "点下班回家了");
}
if (Hour < 21) {
System.out.println("当前时间:" + Hour + "点加班哦,疲劳之极");
} else {
System.out.println("当前时间:" + Hour + "点不行了,睡着了");
}
}
}
}
public class States {
public static void main(String[] args) {
// TODO Auto-generated method stub
Work hour = new Work();
hour.setHour(9);
hour.WriteProgram();
hour.setHour(10);
hour.WriteProgram();
hour.setHour(12);
hour.WriteProgram();
hour.setHour(13);
hour.WriteProgram();
hour.setHour(14);
hour.WriteProgram();
hour.setHour(17);
Work workFinished = new Work();
workFinished.setWorkFinished(true); // 任务完成,可以下班了
hour.WriteProgram();
hour.setHour(19);
hour.WriteProgram();
hour.setHour(22);
hour.WriteProgram();
}
}
二、状态模式
状态模式,当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。
abstract class State {
public abstract void Handle(Context context);
}
class ConcreteStateA extends State {
@Override
public void Handle(Context context) {
// TODO Auto-generated method stub
context.setState(new ConcreteStateB());
}
}
class ConcreteStateB extends State {
@Override
public void Handle(Context context) {
// TODO Auto-generated method stub
context.setState(new ConcreteStateA());
}
}
class Context {
private State state;
public Context(State state) {
super();
this.state = state;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
System.out.println("当前状态:" + state.toString());
}
public void Request() {
state.Handle(this);
}
}
public class States {
public static void main(String[] args) {
// TODO Auto-generated method stub
Context c = new Context(new ConcreteStateA());
c.Request();
c.Request();
c.Request();
c.Request();
}
}
工作状态:状态模式改进
abstract class State { // 抽象状态
public abstract void WriteProgram(Work w);
}
class ForenoonState extends State { // 上午工作状态
@Override
public void WriteProgram(Work w) {
// TODO Auto-generated method stub
if (w.getHour() < 12) {
System.out.println("当前时间:" + w.getHour() + "点上午工作,精神百倍");
} else {
w.setState(new NoonState()); // 超过12点,转入中午工作状态
w.WriteProgram();
}
}
}
class NoonState extends State { // 中午工作类
@Override
public void WriteProgram(Work w) {
// TODO Auto-generated method stub
if (w.getHour() < 13) {
System.out.println("当前时间:" + w.getHour() + "点,饿了,午饭;犯困,午休。");
} else {
w.setState(new AfternoonState()); // 超过13点,转入下午工作状态
w.WriteProgram();
}
}
}
class AfternoonState extends State { // 下午工作类
@Override
public void WriteProgram(Work w) {
// TODO Auto-generated method stub
if (w.getHour() < 17) {
System.out.println("当前时间:" + w.getHour() + "点,下午状态还不错,继续努力");
} else {
w.setState(new EveningState()); // 超过17点,转入傍晚工作状态
w.WriteProgram();
}
}
}
class EveningState extends State { // 晚上工作类
@Override
public void WriteProgram(Work w) {
// TODO Auto-generated method stub
if (w.isFinish()) {
w.setState(new RestState()); // 如果完成任务,转入下班状态
w.WriteProgram();
} else {
if (w.getHour() < 21) {
System.out.println("当前时间:" + w.getHour() + "点加班哦,疲劳之极");
} else {
w.setState(new SleepingState()); // 超过21点,转入睡眠工作状态
}
}
}
}
class SleepingState extends State { // 睡眠状态
@Override
public void WriteProgram(Work w) {
// TODO Auto-generated method stub
System.out.println("当前时间:" + w.getHour() + "点不行了,睡着了");
}
}
class RestState extends State {
@Override
public void WriteProgram(Work w) {
// TODO Auto-generated method stub
System.out.println("当前时间:" + w.getHour() + "点下班回家了");
}
}
class Work { // 工作类
private State current; // 工作初始化
private double hour;
private boolean finish;
public Work() {
current = new ForenoonState();
}
public double getHour() {
return hour;
}
public void setHour(double hour) {
this.hour = hour;
}
public boolean isFinish() {
return finish;
}
public void setFinish(boolean finish) {
this.finish = finish;
}
public void setState(State s) {
current = s;
}
public void WriteProgram() {
current.WriteProgram(this);
}
}
public class States {
public static void main(String[] args) {
// TODO Auto-generated method stub
Work hour = new Work();
hour.setHour(9);
hour.WriteProgram();
hour.setHour(10);
hour.WriteProgram();
hour.setHour(12);
hour.WriteProgram();
hour.setHour(13);
hour.WriteProgram();
hour.setHour(14);
hour.WriteProgram();
hour.setHour(17);
// Work workFinished = new Work();
hour.setFinish(false);// 任务完成,可以下班了
hour.WriteProgram();
hour.setHour(19);
hour.WriteProgram();
hour.setHour(22);
hour.WriteProgram();
}
}