状态模式,例如在一个对象中,用了if/switch这种语句判断,而产生的(方法过长)时,如果以后扩展增加功能的话,就需要去改,if/switch这种语句里面的代码,这样的话就违背了开放封闭式原则, 所以在这种情况下就可以考虑状态模式, 如果判断很简单那就没有必要用状态模式了
面向对象设计中希望做到责任分解
以下就举例(案例),从传统方式,演变成状态模式的过程:
传统方式完成:
package com.oop.demo2;
/**
* 定义工作对象
* @author Liudeli
*
*/
public class Work {
/**
* 定义工作之外的属性
*/
private int hour;
private boolean isComplete;
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public boolean isComplete() {
return isComplete;
}
public void setComplete(boolean isComplete) {
this.isComplete = isComplete;
}
/**
* 做安卓项目开发工作
*/
public void doAndroidProject() {
// 上午九点钟开始上班
if (hour < 12) {
System.out.println(hour + "点钟,上午上班精力旺盛...");
} else if(hour < 13) {
System.out.println(hour + "点钟,中午饿了,该吃饭了...");
} else if (hour <18) {
System.out.println(hour + "点钟,下午犯困,效率降低了...");
} else {
if (isComplete) {
System.out.println(hour + "点钟,下班走人...");
} else {
System.out.println(hour + "点钟,还在熬夜加班中...");
}
}
}
}
package com.oop.demo2;
/**
* 测试程序(改进版本 分类)
* @author Liudeli
*
*/
public class Main {
public static void main(String[] args) {
Work work = new Work();
for (int i = 9; i < 24; i++) {
try {
Thread.sleep(1200);
work.setHour(i);
work.doAndroidProject();
if (20 == i) {
work.setComplete(true);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
运行结果:
演变成状态模式:
package com.oop.demo4;
/**
* 抽象出状态抽象类
*/
public abstract class State {
/**
* 做C项目的工作
* @param work
*/
public abstract void doCProject(Work work);
}
package com.oop.demo4;
/**
* 定义上午状态具体对象
* @author Liudeli
*
*/
public class ShanwuState extends State{
public void doCProject(Work work) {
int hour = work.getHour();
if (hour < 12) {
System.out.println(hour + "点钟,上午工作,精力旺盛...");
} else {
// 切入到中午状态具体对象
work.setState(new ZhonwuState());
// 工作
work.doWork();
}
}
}
package com.oop.demo4;
/**
* 定义中午具体对象
* @author Liudeli
*
*/
public class ZhonwuState extends State{
public void doCProject(Work work) {
int hour = work.getHour();
if (hour < 13) {
System.out.println(hour + "点钟,中午时间到了,该去吃饭了...");
} else {
// 切换到下午状态具体对象
work.setState(new XiawuState());
// 工作
work.doWork();
}
}
}
package com.oop.demo4;
/**
* 定义下午具体对象
* @author Liudeli
*
*/
public class XiawuState extends State{
public void doCProject(Work work) {
int hour = work.getHour();
if (hour < 18) {
System.out.println(hour + "点钟,下午犯困,工作效率降低...");
} else {
// 决定是下班状态
work.setState(new ExitState());
work.doWork();
}
}
}
package com.oop.demo4;
/**
* 定义加班具体对象
* @author Liudeli
*
*/
public class JiabanState extends State{
public void doCProject(Work work) {
boolean exit = work.getExit();
if (exit) {
work.setState(new ExitState());
work.doWork();
} else {
System.out.println(work.getHour() + "点钟,在无休止的加班熬夜中...");
}
}
}
package com.oop.demo4;
/**
* 下班离开状态具体对象
* @author Liudeli
*
*/
public class ExitState extends State {
public void doCProject(Work work) {
boolean exit = work.getExit();
System.out.println(exit);
if (exit) {
System.out.println(work.getHour() + "点钟,工作完成,已下班,准备下班中...");
} else {
work.setState(new JiabanState());
work.doWork();
}
}
}
package com.oop.demo4;
/**
* 定义具体工作对象
* @author Liudeli
*
*/
public class Work {
/**
* 定义抽象状态的抽象类
*/
private State state;
// 默认上午开始工作
public Work() {
this.state = new ShanwuState();
}
public void setState(State state) {
this.state = state;
}
public State getState() {
return state;
}
/**
* 定义工作时间
*/
public int hour;
public void setHour(int hour) {
this.hour = hour;
}
public int getHour() {
return hour;
}
/**
* 定义是否下班
*/
private boolean isExit;
public boolean getExit() {
return isExit;
}
public void setExit(boolean isExit) {
this.isExit = isExit;
}
/**
* 执行工作
*/
public void doWork() {
state.doCProject(this);
}
}
/**
* 测试程序(状态模式)
* @author Liudeli
*
*/
public class Test {
public static void main(String[] args) {
Work work = new Work();
for (int i = 9; i < 24; i++) {
try {
Thread.sleep(1200);
work.setHour(i);
if (19 < i) {
work.setExit(true);
}
work.doWork();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
运行结果:
谢谢大家的观看,更多精彩技术博客,会不断的更新,请大家访问,
刘德利CSDN博客, http://blog.csdn.net/u011967006