状态模式:将类(状态机器)的各种状态抽象成状态类,定义成属性,根据类执行的不同行为,在状态类中转换状态,这样去除了类中根据各种状态判断(if else)来执行对应的行为,由对应的状态类进行处理,应用了组合委托,由于状态类含有类的引用,所以可以达到切换状态和执行其想要的行为。
状态接口
public interface State {
void leaveOffice();
void preparingInterview();
void willInterview();
void onInterview();
void waitForOffer();
void working();
}
java程序员类(状态机器)
package programmer;
public class JavaProgrammer {
private float salary = 8000;
State leaveOfficeState;
State preparingInterviewState;
State willInterviewState;
State onInterviewState;
State waitOfferState;
State workingState;
State mState;
public JavaProgrammer() {
leaveOfficeState = new LeaveOfficeState(this);
preparingInterviewState = new PreparingInterviewState(this);
willInterviewState = new WillInterviewState(this);
onInterviewState = new OnIterviewState(this);
waitOfferState = new WaitOfferState(this);
workingState = new WorkingState(this);
mState = leaveOfficeState;
}
public void leaveOffice() {
System.out.println("This coding place is not where i want , i will leave away . please forget i have been here.");
System.out.println("have to go , that is my freedom");
mState.leaveOffice();
}
public void preparingInterview() {
System.out.println("some time ago ,An company called me ,'hi cly , could you ... ");
if (salary >= 8000) {
System.out.println("ok ,please email to me'");
mState.preparingInterview();
} else {
System.out.println("sorry , please hold on ,your called phone is power off...");
}
}
public void willInterview() {
System.out.println("review java ee and java Basic");
System.out.println("print resume");
System.out.println("read information of the company in the subway");
mState.willInterview();
}
public void onIterview() {
System.out.println("please introduce yourself");
System.out.println("what is Servlet? and so on");
mState.onInterview();
}
public void waitOffer() {
System.out.println("i am here waiting for offer");
mState.waitForOffer();
}
public void working() {
System.out.println("don't break me , i am in the working mode");
mState.working();
}
public State getState() {
System.out.println("state===========>"+mState.getClass().getName());
return mState;
}
public void setState(State mState) {
this.mState = mState;
}
}
离职状态类
package programmer;
public class LeaveOfficeState extends BaseState implements State{
public LeaveOfficeState(JavaProgrammer programmer){
super(programmer);
}
@Override
public void leaveOffice() {
// TODO Auto-generated method stub
programmer.setState(programmer.preparingInterviewState);
}
@Override
public void preparingInterview() {
// TODO Auto-generated method stub
}
@Override
public void willInterview() {
// TODO Auto-generated method stub
}
@Override
public void waitForOffer() {
// TODO Auto-generated method stub
}
@Override
public void onInterview() {
// TODO Auto-generated method stub
}
@Override
public void working() {
// TODO Auto-generated method stub
}
}
准备面试类
package programmer;
public class PreparingInterviewState extends BaseState implements State{
public PreparingInterviewState(JavaProgrammer programmer) {
super(programmer);
// TODO Auto-generated constructor stub
}
@Override
public void leaveOffice() {
// TODO Auto-generated method stub
}
@Override
public void preparingInterview() {
// TODO Auto-generated method stub
programmer.setState(programmer.willInterviewState);
}
@Override
public void willInterview() {
// TODO Auto-generated method stub
}
@Override
public void onInterview() {
// TODO Auto-generated method stub
}
@Override
public void waitForOffer() {
// TODO Auto-generated method stub
}
@Override
public void working() {
// TODO Auto-generated method stub
}
}
其他实现类省掉。。。
客户端类
package programmer;
public class Demo {
public static void main(String[] args) {
JavaProgrammer jp = new JavaProgrammer();
jp.getState();
jp.leaveOffice();
jp.getState();
jp.preparingInterview();
jp.getState();
jp.willInterview();
jp.getState();
jp.onIterview();
jp.getState();
jp.waitOffer();
jp.getState();
jp.working();
jp.getState();
}
}
打印结果:
state===========>programmer.LeaveOfficeState
This coding place is not where i want , i will leave away . please forget i have been here.
have to go , that is my freedom
state===========>programmer.PreparingInterviewState
some time ago ,An company called me ,'hi cly , could you ...
ok ,please email to me'
state===========>programmer.WillInterviewState
review java ee and java Basic
print resume
read information of the company in the subway
state===========>programmer.OnIterviewState
please introduce yourself
what is Servlet? and so on
state===========>programmer.WaitOfferState
i am here waiting for offer
state===========>programmer.WorkingState
don't break me , i am in the working mode