Curriculum design patterns Design patterns 27-2 succinctly state mode coding

1 Code Walkthrough

1.1 Code Walkthrough 1

 

 

 

 

1 Code Walkthrough

1.1 Code Walkthrough 1

demand:

There are courses video playback, fast forward, pause, stop (closed) four states, you can switch between each state, but the state can not switch to stop fast-forward and pause state

 

 

:( focus on personal development did not notice the place)

Core 1: context class: this.courseVideoState.setCourseVideoContext (this); State directly equivalent to the current state of itself directly into the context (CourseVideoContext) in

Key 2: Status base class: because the properties for a subclass to be used, provided it is protected permissions

 

 

uml 类图:

 

 

Test categories:

package com.geely.design.pattern.behavioral.state;

public class Test {

    @org.junit.Test
    public void testVideoState(){

        CourseVideoContext courseVideoContext = new CourseVideoContext();
        courseVideoContext.setCourseVideoState(new PlayState());

        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();
        System.out.println("目前状态为:"+courseVideoContext.getCourseVideoState().getClass().getSimpleName());
    }
}

 

 

 

视频状态基类:

package com.geely.design.pattern.behavioral.state;

public abstract class CourseVideoState {
    //重点:因为要被子类所使用的属性,所以权限设置成protected
    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;

public class CourseVideoContext {
    private CourseVideoState courseVideoState;
    public final static PlayState PLAY_STATE = new PlayState();
    public final static PauseState PAUSE_STATE = new PauseState();
    public final static SpeedState SPEED_STATE = new SpeedState();
    public final static StopState STOP_STATE = new StopState();

    //注意这里是get方法,不是构造器
    public CourseVideoState getCourseVideoState() {
        return courseVideoState;
    }

    /**
     * 核心:重点:this.courseVideoState.setCourseVideoContext(this); 相当于把State 直接把 当前本身状态 直接放到 上下文(CourseVideoContext)中
     * @param courseVideoState
     */
    public void setCourseVideoState(CourseVideoState courseVideoState) {
        this.courseVideoState = courseVideoState;
        this.courseVideoState.setCourseVideoContext(this);
    }

    public void Play(){
        this.courseVideoState.play();
    }

    public void Pause(){
        this.courseVideoState.pause();
    }

    public void Speed(){
        this.courseVideoState.speed();
    }

    public void Stop(){
        this.courseVideoState.stop();
    }
}

 

播放状态类:

package com.geely.design.pattern.behavioral.state;

public class PlayState extends CourseVideoState {
    @Override
    public void play() {
        System.out.println("开始播放");
    }

    @Override
    /**
     * 此处方法需要仔细看
     * 通过 上下文来设置课程视频状态
     */
    public void speed() {
        super.courseVideoContext.setCourseVideoState(CourseVideoContext.SPEED_STATE);
    }

    @Override
    public void pause() {
        super.courseVideoContext.setCourseVideoState(CourseVideoContext.PAUSE_STATE);
    }

    @Override
    public void stop() {
        super.courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE);
    }
}

 

暂停状态类:

package com.geely.design.pattern.behavioral.state;

public class PauseState extends CourseVideoState {
    @Override
    public void play() {
        super.courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE);
    }

    @Override
    public void speed() {
        super.courseVideoContext.setCourseVideoState(CourseVideoContext.SPEED_STATE);
    }

    @Override
    public void pause() {
        System.out.println("处于暂停状态");
    }

    @Override
    public void stop() {
        super.courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE);
    }
}

 

快进状态类:

package com.geely.design.pattern.behavioral.state;

public class SpeedState extends CourseVideoState {
    @Override
    public void play() {
        super.courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE);
    }

    @Override
    public void speed() {
        System.out.println("处于快进状态");
    }

    @Override
    public void pause() {
        super.courseVideoContext.setCourseVideoState(CourseVideoContext.PAUSE_STATE);
    }

    @Override
    public void stop() {
        super.courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE);
    }
}

 

停止状态类:

package com.geely.design.pattern.behavioral.state;

public class StopState extends CourseVideoState {
    @Override
    public void play() {
        super.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("已关闭");
    }
}

 

 

打印日志:

目前状态为:PlayState
目前状态为:PauseState
目前状态为:SpeedState
目前状态为:StopState
ERROR 关闭状态 不能切换到 快进状态
目前状态为:StopState

Process finished with exit code 0

 

Guess you like

Origin www.cnblogs.com/1446358788-qq/p/12440907.html