行为模式之状态模式

2019-09-18  本文已影响0人  后来丶_a24d

总纲

状态模式

uml类图

状态模式uml图.png

状态模式代码

public class CommonState implements RunState{

        public void run(Hero hero) {
            //正常跑动则不打印内容,否则会刷屏
        }

    }

    public class SpeedUpState implements RunState{

        public void run(Hero hero) {
            System.out.println("--------------加速跑动---------------");
            try {
                Thread.sleep(4000);//假设加速持续4秒
            } catch (InterruptedException e) {}
            hero.setState(Hero.COMMON);
            System.out.println("------加速状态结束,变为正常状态------");
        }

    }

    public class SpeedDownState implements RunState{

        public void run(Hero hero) {
            System.out.println("--------------减速跑动---------------");
            try {
                Thread.sleep(4000);//假设减速持续4秒
            } catch (InterruptedException e) {}
            hero.setState(Hero.COMMON);
            System.out.println("------减速状态结束,变为正常状态------");
        }

    }

    public class SwimState implements RunState{

        public void run(Hero hero) {
            System.out.println("--------------不能跑动---------------");
            try {
                Thread.sleep(2000);//假设眩晕持续2秒
            } catch (InterruptedException e) {}
            hero.setState(Hero.COMMON);
            System.out.println("------眩晕状态结束,变为正常状态------");
        }

    }

    //英雄类
    public class Hero {

        public static final RunState COMMON = new CommonState();//正常状态

        public static final RunState SPEED_UP = new SpeedUpState();//加速状态

        public static final RunState SPEED_DOWN = new SpeedDownState();//减速状态

        public static final RunState SWIM = new SwimState();//眩晕状态

        private RunState state = COMMON;//默认是正常状态

        private Thread runThread;//跑动线程

        //设置状态
        public void setState(RunState state) {
            this.state = state;
        }
        //停止跑动
        public void stopRun() {
            if (isRunning()) runThread.interrupt();
            System.out.println("--------------停止跑动---------------");
        }
        //开始跑动
        public void startRun() {
            if (isRunning()) {
                return;
            }
            final Hero hero = this;
            runThread = new Thread(new Runnable() {
                public void run() {
                    while (!runThread.isInterrupted()) {
                        state.run(hero);
                    }
                }
            });
            System.out.println("--------------开始跑动---------------");
            runThread.start();
        }

        private boolean isRunning(){
            return runThread != null && !runThread.isInterrupted();
        }

    }

    public class Main {

        public static void main(String[] args) throws InterruptedException {
            Hero hero = new Hero();
            hero.startRun();
            hero.setState(Hero.SPEED_UP);
            Thread.sleep(5000);
            hero.setState(Hero.SPEED_DOWN);
            Thread.sleep(5000);
            hero.setState(Hero.SWIM);
            Thread.sleep(5000);
            hero.stopRun();
        }
    }
上一篇下一篇

猜你喜欢

热点阅读