定义:
状态模式是一种对象行为型模式,是指将有状态的对象,把复杂的“判断逻辑”提取到不同的状态对象中,允许状态对象在其内部状态发生改变时改变行为。
思想:
当控制一个对象状态转换的表达式过于复杂时,将相关“判断逻辑”提取出来,放到一系列的状态类中,这样可以把原来复杂的逻辑判断简单化
缺点:
在原有的基础上增加了系统的类和对象。
结构和实现较为复杂。
以线程的生命周期为例。
代码:
package com.hh;
public class ThreadContext {
private ThreadState threadState;
public ThreadContext() {
threadState=new NewState();
}
public ThreadState getThreadState() {
return threadState;
}
public void setThreadState(ThreadState threadState) {
this.threadState=threadState;
}
public void start(){
((NewState)threadState).start(this);
}
public void getCpu() {
((RunnableState)threadState).getCpu(this);
}
public void suspend() {
((RunningState)threadState).suspend(this);
}
public void stop() {
((RunningState)threadState).stop(this);
}
public void resume() {
((BlockedState)threadState).resume(this);
}
}
package com.hh;
public abstract class ThreadState {
protected int state;//状态名
}
package com.hh;
public class StateSet {
public static final int RUNNING = 0;
public static final int RUNNABLE = 0;
public static final int BLOCKED = 0;
public static final int NEW = 0;
public static final int DEAD = 0;
}
package com.hh;
public class RunningState extends ThreadState{
public RunningState() {
state=StateSet.RUNNING;
System.out.println("运行状态");
}
public void suspend(ThreadContext tc) {
System.out.println("调用suspend方法");
if(state==StateSet.RUNNING)
tc.setThreadState(new BlockedState());
else
System.out.println("当前状态不是运行状态");
}
public void stop(ThreadContext tc) {
System.out.println("调用stop方法");
if(state==StateSet.RUNNING)
tc.setThreadState(new DeadState());
else
System.out.println("当前状态不是运行状态");
}
}
package com.hh;
public class NewState extends ThreadState {
public NewState() {
state=StateSet.NEW;
System.out.println("新建线程");
}
public void start(ThreadContext tc) {
System.out.println("调用了Start方法");
if(state==StateSet.NEW)
tc.setThreadState(new RunnableState());
else
System.out.println("当前线程不是新建状态");
}
}
package com.hh;
public class RunnableState extends ThreadState {
public RunnableState() {
state=StateSet.RUNNABLE;
System.out.println("就绪状态");
}
public void getCpu(ThreadContext tc) {
// TODO Auto-generated method stub
System.out.println("调用getCpu方法");
if(state==StateSet.RUNNABLE)
tc.setThreadState(new RunningState());
else
System.out.println("当前状态不是就绪状态");
}
}
package com.hh;
public class BlockedState extends ThreadState {
public BlockedState() {
state=StateSet.BLOCKED;
System.out.println("阻塞线程");
}
public void resume(ThreadContext tc) {
System.out.println("调用了resume方法");
if(state==StateSet.BLOCKED)
tc.setThreadState(new RunnableState());
else
System.out.println("当前线程不是新建状态");
}
}
package com.hh;
public class DeadState extends ThreadState {
public DeadState() {
state=StateSet.DEAD;
System.out.println("就绪状态");
}
public void stop(ThreadContext tc) {
// TODO Auto-generated method stub
System.out.println("调用getCpu方法");
if(state==StateSet.DEAD)
tc.setThreadState(new DeadState());
else
System.out.println("当前状态死亡");
}
}
package com.hh;
public class ClientClass {
public static void main(String args []) {
ThreadContext tc=new ThreadContext();
tc.start();
tc.getCpu();
tc.suspend();
}
}
运行结果:
如果修改一下客户端代码 在阻塞前stop线程:
报错,状态不符,不可以执行stop操作,体现为不能进行状态转换。
学习视频指路:黑手书生
https://www.bilibili.com/video/av95698793?from=search&seid=5012547147156938065