美文网首页
状态模式

状态模式

作者: wbpailxt | 来源:发表于2019-12-01 20:17 被阅读0次

    一个对象存在多个状态(不同状态下行为不同),且状态可相互转换


    图片.png

    状态类:

    package com.geely.design.pattern.behavioral.state;
    
    /**
     * @Author: wenbaipei
     * @Date: 2019/11/30 19:36
     * @Version 1.0
     */
    public abstract class CourseVideoState {
        protected CourseVideoContext courseVideoContext;
        public void setCourseVideoContext(CourseVideoContext courseVideoContext) {
            this.courseVideoContext = courseVideoContext;
        }
        public abstract void play();
        public abstract void speed();
        public abstract void pause();
        public abstract void stop();
    
    }
    
    package com.geely.design.pattern.behavioral.state;
    
    /**
     * @Author: wenbaipei
     * @Date: 2019/11/30 19:39
     * @Version 1.0
     */
    public class PlayState extends CourseVideoState {
        @Override
        public void play() {
            System.out.println("正常播放课程视频状态");
        }
    
        @Override
        public void speed() {
            courseVideoContext.setCourseVideoState(CourseVideoContext.SPEED_STATE);
        }
    
        @Override
        public void pause() {
            courseVideoContext.setCourseVideoState(CourseVideoContext.PAUSE_STATE);
        }
    
        @Override
        public void stop() {
            courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE);
        }
    }
    
    package com.geely.design.pattern.behavioral.state;
    
    /**
     * @Author: wenbaipei
     * @Date: 2019/11/30 19:39
     * @Version 1.0
     */
    public class SpeedState extends CourseVideoState  {
        @Override
        public void play() {
            courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE);
        }
    
        @Override
        public void speed() {
            System.out.println("快进播放课程视频状态");
        }
    
        @Override
        public void pause() {
            courseVideoContext.setCourseVideoState(CourseVideoContext.PAUSE_STATE);
        }
    
        @Override
        public void stop() {
            courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE);
        }
    }
    
    
    /**
     * @Author: wenbaipei
     * @Date: 2019/11/30 19:39
     * @Version 1.0
     */
    public class PauseState extends CourseVideoState  {
        @Override
        public void play() {
            courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE);
        }
    
        @Override
        public void speed() {
            courseVideoContext.setCourseVideoState(CourseVideoContext.SPEED_STATE);
        }
    
        @Override
        public void pause() {
            System.out.println("暂停播放课程视频状态");
        }
    
        @Override
        public void stop() {
            courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE);
        }
    }
    
    package com.geely.design.pattern.behavioral.state;
    
    /**
     * @Author: wenbaipei
     * @Date: 2019/11/30 19:39
     * @Version 1.0
     */
    public class StopState extends CourseVideoState  {
        @Override
        public void play() {
            courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE);
        }
    
        @Override
        public void speed() {
            System.out.println("ERROR 停止状态不能快进");
        }
    
        @Override
        public void pause() {
            System.out.println("ERROR 停止状态不能暂停");
        }
    
        @Override
        public void stop() {
            System.out.println("停止播放课程视频状态");
        }
    }
    

    上下文状态:

    package com.geely.design.pattern.behavioral.state;
    
    /**
     * @Author: wenbaipei
     * @Date: 2019/11/30 19:37
     * @Version 1.0
     */
    public class CourseVideoContext {
        private CourseVideoState courseVideoState;
        public final static PlayState PLAY_STATE = new PlayState();
        public final static SpeedState SPEED_STATE = new SpeedState();
        public final static PauseState PAUSE_STATE = new PauseState();
        public final static StopState STOP_STATE = new StopState();
    
        public CourseVideoState getCourseVideoState() {
            return courseVideoState;
        }
    
        public void setCourseVideoState(CourseVideoState courseVideoState) {
            this.courseVideoState = courseVideoState;
            this.courseVideoState.setCourseVideoContext(this);
        }
        public void play(){
            this.courseVideoState.play();
        }
        public void speed(){
            this.courseVideoState.speed();
        }
        public void pause(){
            this.courseVideoState.pause();
        }
        public void stop(){
            this.courseVideoState.stop();
        }
    }
    

    测试类:

    package com.geely.design.pattern.behavioral.state;
    
    /**
     * @Author: wenbaipei
     * @Date: 2019/11/30 19:54
     * @Version 1.0
     */
    public class Test {
        public static void main(String[] args) {
            CourseVideoContext courseVideoContext = new CourseVideoContext();
            courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE);
    
            System.out.println("当前状态:"+courseVideoContext.getCourseVideoState().getClass().getSimpleName());
            courseVideoContext.pause();
    
            System.out.println("当前状态:"+courseVideoContext.getCourseVideoState().getClass().getSimpleName());
            courseVideoContext.speed();
    
            System.out.println("当前状态:"+courseVideoContext.getCourseVideoState().getClass().getSimpleName());
            courseVideoContext.stop();
            System.out.println("当前状态:"+courseVideoContext.getCourseVideoState().getClass().getSimpleName());
    
            courseVideoContext.speed();
        }
    }
    

    结果:


    图片.png

    相关文章

      网友评论

          本文标题:状态模式

          本文链接:https://www.haomeiwen.com/subject/jshtgctx.html