第16章 无尽加班何时休 --状态模式

情境:一天的工作状态:上午状态好,中午想睡觉,下午渐恢复,加班苦煎熬。

工作状态-1.0

work类

public class Work {

    private int hour;

    private boolean finish = false;

    public void writeProgram() {
        if (hour < 12) {
            print("当前时间:" + hour + "点,精神百倍");
        } else if (hour < 13) {
            print("当前时间:" + hour + "点,该吃饭了");
        } else if (hour < 17) {
            print("当前时间:" + hour + "点,下午状态还不错,加油!");
        } else {
            if(finish){
                print("当前时间:" + hour + "点,下班啦!");
            }else {
                if(hour < 21){
                    print("当前时间:" + hour + "点,加班哦,累!");
                }else {
                    print("当前时间:" + hour + "点,不行了,睡着了!");
                }
            }
        }
    }

    public int getHour() {
        return hour;
    }

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

    public boolean isFinish() {
        return finish;
    }

    public void setFinish(boolean finish) {
        this.finish = finish;
    }
}

测试代码

public class Test {

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

        work.setFinish(true);
        work.writeProgram();

        work.setFinish(false);
        work.setHour(19);
        work.writeProgram();
        work.setHour(22);
        work.writeProgram();
    }

}

结果


图片.png

面向对象设计其实就是希望做到代码的责任分解,Work类writeProgram方法有很多判断分支,这就意味着它的职责过大,违背了‘单一职责原则。’

状态模式

状态模式(State),当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。[DP]

状态模式主要解决的是当控制一个对象状态转换的条件表达式过于复杂时的情况,把状态的判断逻辑转移到表示不同状态的一系列类当中,可以把复杂的判断逻辑简化。

状态模式(State)结构图
图片.png

State类,抽象状态类,定义一个接口以封装与Context的一个特定状态相关的行为

public abstract class State {

    public abstract void handle(Context context);

}

ConcreteState类,具体状态类,每一个子类实现一个与Context的一个状态相关的行为

public class ConcreteStateA extends State {
    @Override
    public void handle(Context context) {
        //设置A的下一个状态B
        context.setState(new ConcreteStateB());
    }
}

public class ConcreteStateB extends State {
    @Override
    public void handle(Context context) {
        //设置B的下一个状态A
        context.setState(new ConcreteStateA());
    }
}

Context类,维护一个ConcreteState子类的实例,这个实例定义当前的状态

public class Context {

    private State state;

    public void request(){
        state.handle(this);
    }

    public Context(State state){
        this.state = state;
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
        print("当前状态:"+state.getClass().getName());
    }
}

测试代码

public class Test {

    public static void main(String[] args) {
        Context context = new Context(new ConcreteStateA());
        //不断地请求,同时更改状态
        context.request();
        context.request();
        context.request();
        context.request();
    }

}
状态模式好处与用处

状态模式的好处是将与特定状态相关的行为局部化,并且将不同状态的行为分割开来[DP]
将特定的状态相关的行为都放入一个对象中,由于所有与状态相关的代码都存在于某个ConcreteState中,所以通过定义新的子类可以很容易地增加新的状态和转换[DP]
简单来说,就是为了消除庞大的条件分支语句
状态模式通过把各种状态转移逻辑分布到State的子类之间,来减少相互间的依赖。
当一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为时,就可以考虑使用状态模式了。

工作状态-2.0

将状态分为:上午工作状态、中午工作状态、下午工作状态、傍晚工作状态、下班状态、睡眠状态

抽象状态类,定义一个抽象方法“写程序”

public abstract class State {

    public abstract void writeProgram(Work work);

}

具体状态类

public class ForenoonState extends State {

    @Override
    public void writeProgram(Work work) {
        int hour = work.getHour();
        if(hour < 12){
            print("当前时间:" + hour + "点,精神百倍");
        }else {
            //超过12点,则转入中午工作状态
            work.setState(new NoonState());
            work.writeProgram();
        }
    }
}

public class NoonState extends State {

    @Override
    public void writeProgram(Work work) {
        int hour = work.getHour();
        if(hour < 13){
            print("当前时间:" + hour + "点,该吃饭了");
        }else {
            //超过13点,则转入下午工作状态
            work.setState(new AfternoonState());
            work.writeProgram();
        }
    }
}

public class AfternoonState extends State {

    @Override
    public void writeProgram(Work work) {
        int hour = work.getHour();
        if(hour < 17){
            print("当前时间:" + hour + "点,下午状态还不错,加油!");
        }else {
            //超过17点,则转入傍晚工作状态
            work.setState(new EveningState());
            work.writeProgram();
        }
    }
}

public class EveningState extends State {

    @Override
    public void writeProgram(Work work) {
        int hour = work.getHour();
        if(work.isFinish()){
            //工作完成,进入下班状态
            work.setState(new RestState());
            work.writeProgram();
        }else {
            if(hour < 21){
                print("当前时间:" + hour + "点,加班,累!");
            }else {
                //超过21点,进入睡眠状态
                work.setState(new SleepingState());
                work.writeProgram();
            }
        }
    }
}

public class RestState extends State {

    @Override
    public void writeProgram(Work work) {
        print("当前时间:" + work.getHour() + "点,下班啦!");
    }
}

public class SleepingState extends State {

    @Override
    public void writeProgram(Work work) {
        int hour = work.getHour();
        print("当前时间:" + hour + "点,不行了,睡着了!");
    }
}

工作类

public class Work {

    private State state;

    private int hour;

    private boolean finish = false;

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

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

    public int getHour() {
        return hour;
    }

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

    public boolean isFinish() {
        return finish;
    }

    public void setFinish(boolean finish) {
        this.finish = finish;
    }

    public State getState() {
        return state;
    }

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

测试代码

public class Test {

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

        work.setFinish(false);
        work.setHour(19);
        work.writeProgram();
        work.setHour(22);
        work.writeProgram();
    }

}

你可能感兴趣的:(第16章 无尽加班何时休 --状态模式)