为了账号安全,请及时绑定邮箱和手机立即绑定

状态模式

标签:
Java

一个对象存在多个状态(不同状态下行为不同),且状态可相互转换
图片.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

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消