美文网首页
状态模式

状态模式

作者: 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

相关文章

  • State模式

    状态模式(State模式) 定义 状态模式,又称状态对象模式(Pattern of Objects for S...

  • 设计模式-状态模式

    设计模式-状态模式 设计模式 状态模式的关键是区分事物内部的状态

  • 状态模式(状态机模式)

    状态模式学习笔记 前言:文章从三方面简单阐述状态模式:是什么、为什么、如何做。这是我在工作之余自己的一些理解、思考...

  • C++设计模式(3)

    本文预览: 状态模式 组合模式 迭代器 职责链 命令 状态模式 定义:状态模式(State Pattern),允许...

  • 设计模式——状态模式

    定义 状态模式,又称状态对象模式(Pattern of Objects for States),状态模式是对象的行...

  • 第5章 -行为型模式-状态模式

    一、状态模式的简介 二、状态模式的优缺点 三、状态模式的实例

  • 状态模式

    Android进阶之设计模式 状态模式 定义: 当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了...

  • 状态模式

    状态模式:允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。允许对象随着状态改变而改变行为。 策略...

  • 状态模式

    《大话设计模式》阅读笔记和总结。原书是C#编写的,本人用Java实现了一遍,包括每种设计模式的UML图实现和示例代...

  • 状态模式

    状态模式 一个对象有状态变化 每次状态变化都会触发一个逻辑 不能总是用 if...else 来控制 示例 交通灯信...

网友评论

      本文标题:状态模式

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