状态模式

package com.design;

/** 状态模式 如果使用 if ... else if(){}else if(){}超过三层建议使用
 * Created by nicknailo on 2018/6/30.
 */
public class StatePattern {

    public static void main(String[] args) {
        Work work = new Work();
        work.setHour( 9 );
        work.writeProgram();

        System.out.println("------------------");
        work.setHour( 10 );
        work.writeProgram();
        System.out.println("------------------");
        work.setHour( 12 );
        work.writeProgram();
        System.out.println("------------------");
        work.setHour( 13 );
        work.writeProgram();
        System.out.println("------------------");
        work.setHour( 14 );
        work.writeProgram();
        System.out.println("------------------");
        work.setHour( 17 );
        work.writeProgram();
//        work.setFinished( true );
        work.setFinished( false );
        System.out.println("------------------");
        work.writeProgram();
        work.setHour( 19 );
        work.writeProgram();
        work.setHour( 22 );
        work.writeProgram();
    }


}

interface State{
    void writeProgram(Work w);
}

class ForenoonState implements State{
    @Override
    public void writeProgram(Work w) {
        if(w.getHour() < 12){
            System.out.println("当前时间 + "+ w.getHour()  +"点 上午工作,精神百倍  ");
        }else{
            w.setState( new NoonState() );
            w.writeProgram();
        }
    }
}

class NoonState implements State{
    @Override
    public void writeProgram(Work w) {
        if(w.getHour() < 13){
            System.out.println("当前时间 + "+ w.getHour()  +"点 饿了,午饭,犯困 午休  ");
        }else {
            w.setState( new AfternoonState() );
            w.writeProgram();
        }
    }
}

class AfternoonState implements State{
    @Override
    public void writeProgram(Work w) {
        if(w.getHour() < 17){
            System.out.println("当前时间 + "+ w.getHour()  +"点 下午工作,状态还不错,继续努力  ");
        }else {
            w.setState( new EveningState() );
            w.writeProgram();
        }
    }
}

class EveningState implements State{
    @Override
    public void writeProgram(Work w) {
        if(w.isFinished()){
            w.setState( new RestState() );
            w.writeProgram();
        }else{
            if(w.getHour() < 21){
                System.out.println("当前时间 + "+ w.getHour()  +"点 加班哦,疲惫至极  ");
            }else {
                w.setState( new SleepingState() );
                w.writeProgram();
            }
        }

    }
}

class SleepingState implements State{
    @Override
    public void writeProgram(Work w) {
            System.out.println("当前时间 + "+ w.getHour()  +"点 不行了,要睡觉  ");
    }
}

class RestState implements State{
    @Override
    public void writeProgram(Work w) {
            System.out.println("当前时间 + "+ w.getHour()  +"点 下班回家了  ");
    }
}

class Work{
    private State state;

    public Work() {
        this.state = new ForenoonState();
    }

    private double hour;
    private boolean finished = false;

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public boolean isFinished() {
        return finished;
    }

    public void setFinished(boolean finished) {
        this.finished = finished;
    }

    public double getHour() {
        return hour;
    }

    public void setHour(double hour) {
        this.hour = hour;
    }

    public void writeProgram(){
        state.writeProgram(this);
    }


}

 运行结果:

  

 

posted @ 2018-08-21 14:37  nicknailo  阅读(141)  评论(0编辑  收藏  举报