package state
var PLAY_STATE = PlayState{}
var STOP_STATE = StopState{}
var PAUSE_STATE = PauseState{}
var SPEED_STATE = SpeedState{}
type VideoContext struct {
videoState IVideoState
}
func NewVideoContext() *VideoContext {
v := &VideoContext{}
PLAY_STATE.videoContext = v
PAUSE_STATE.videoContext = v
STOP_STATE.videoContext = v
SPEED_STATE.videoContext = v
return v
}
func (v *VideoContext) SetVideoState(videoState IVideoState) {
v.videoState = videoState
}
func (v *VideoContext) play() {
v.videoState.play()
}
func (v *VideoContext) stop() {
v.videoState.stop()
}
func (v *VideoContext) pause() {
v.videoState.pause()
}
func (v *VideoContext) speed() {
v.videoState.speed()
}
package state
import "fmt"
type SpeedState struct {
videoContext *VideoContext
}
func (s SpeedState) play() {
s.videoContext.videoState = PLAY_STATE
}
func (s SpeedState) stop() {
s.videoContext.videoState = STOP_STATE
}
func (s SpeedState) pause() {
s.videoContext.videoState = PAUSE_STATE
}
func (s SpeedState) speed() {
fmt.Println("快进播放Video")
}
package state
import "fmt"
type StopState struct {
videoContext *VideoContext
}
func (s StopState) play() {
s.videoContext.videoState = PLAY_STATE
}
func (s StopState) stop() {
fmt.Println("停止播放Video")
}
func (s StopState) pause() {
fmt.Println("ERROR 停止状态不能 暂停")
}
func (s StopState) speed() {
fmt.Println("ERROR 停止状态不能 快进")
}
package state
import (
"fmt"
"reflect"
)
func ExampleState() {
videoContext := NewVideoContext()
videoContext.SetVideoState(PLAY_STATE)
fmt.Printf("current state : %v \n", reflect.TypeOf(videoContext.videoState).Name())
videoContext.pause()
fmt.Printf("current state : %v \n", reflect.TypeOf(videoContext.videoState).Name())
videoContext.speed()
fmt.Printf("current state : %v \n", reflect.TypeOf(videoContext.videoState).Name())
videoContext.stop()
fmt.Printf("current state : %v \n", reflect.TypeOf(videoContext.videoState).Name())
videoContext.speed()
fmt.Printf("current state : %v \n", reflect.TypeOf(videoContext.videoState).Name())
//Output:
// current state : PlayState
// current state : PauseState
// current state : SpeedState
// current state : StopState
// ERROR 停止状态不能 快进
// current state : StopState
}
package tech.selinux.design.pattern.behavioral.state;
public abstract class VideoState {
protected VideoContext videoContext;
public void setVideoContext(VideoContext videoContext) {
this.videoContext = videoContext;
}
public abstract void play();
public abstract void speed();
public abstract void pause();
public abstract void stop();
}
package tech.selinux.design.pattern.behavioral.state;
public class VideoContext {
private VideoState videoState;
public static final PlayState PLAY_STATE = new PlayState();
public static final StopState STOP_STATE = new StopState();
public static final PauseState PAUSE_STATE = new PauseState();
public static final SpeedState SPEED_STATE = new SpeedState();
public VideoState getVideoState() {
return videoState;
}
public void setVideoState(VideoState videoState) {
this.videoState = videoState;
// 把当前的环境设置到上下文
this.videoState.setVideoContext(this);
}
public void play() {
this.videoState.play();
}
public void stop() {
this.videoState.stop();
}
public void pause() {
this.videoState.pause();
}
public void speed() {
this.videoState.speed();
}
}
package tech.selinux.design.pattern.behavioral.state;
/** play 状态下是可以切换到其他状态的 */
public class PlayState extends VideoState {
@Override
public void play() {
System.out.println("正常播放Video");
}
@Override
public void speed() {
super.videoContext.setVideoState(VideoContext.SPEED_STATE);
}
@Override
public void pause() {
super.videoContext.setVideoState(VideoContext.PAUSE_STATE);
}
@Override
public void stop() {
super.videoContext.setVideoState(VideoContext.STOP_STATE);
}
}
package tech.selinux.design.pattern.behavioral.state;
/** 暂停状态能够切换到其他状态 */
public class PauseState extends VideoState {
@Override
public void play() {
super.videoContext.setVideoState(VideoContext.PLAY_STATE);
}
@Override
public void speed() {
super.videoContext.setVideoState(VideoContext.SPEED_STATE);
}
@Override
public void pause() {
System.out.println("暂停播放Video");
}
@Override
public void stop() {
super.videoContext.setVideoState(VideoContext.STOP_STATE);
}
}
package tech.selinux.design.pattern.behavioral.state;
/** 快进状态下是可以切换到其他状态的。 */
public class SpeedState extends VideoState {
@Override
public void play() {
super.videoContext.setVideoState(VideoContext.PLAY_STATE);
}
@Override
public void speed() {
System.out.println("快进播放Video");
}
@Override
public void pause() {
super.videoContext.setVideoState(VideoContext.PAUSE_STATE);
}
@Override
public void stop() {
super.videoContext.setVideoState(VideoContext.STOP_STATE);
}
}
package tech.selinux.design.pattern.behavioral.state;
/** 停止状态不能切换到其他状态 */
public class StopState extends VideoState {
@Override
public void play() {
super.videoContext.setVideoState(VideoContext.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("停止播放Video");
}
}
package tech.selinux.design.pattern.behavioral.state;
public class Test {
public static void main(String[] args) {
VideoContext videoContext = new VideoContext();
// 设置初始状态
videoContext.setVideoState(new PlayState());
System.out.println("current state :" + videoContext.getVideoState().getClass().getSimpleName());
videoContext.pause();
System.out.println("current state :" + videoContext.getVideoState().getClass().getSimpleName());
videoContext.speed();
System.out.println("current state :" + videoContext.getVideoState().getClass().getSimpleName());
videoContext.stop();
System.out.println("current state :" + videoContext.getVideoState().getClass().getSimpleName());
videoContext.speed();
System.out.println("current state :" + videoContext.getVideoState().getClass().getSimpleName());
}
}