本文为笔者学习《Head First设计模式》的笔记,并加入笔者自己的理解和归纳总结
状态模式允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。
结构图
状态图
播放器(MediaPlayer
)
- 最初状态(
PrepareState
),点击播放可进入播放状态(PlayState
)。 - 播放状态(
PlayState
)点击暂停可进入暂停状态(PauseState
),或者点击停止进入停止状态(StopState
)。 - 暂停状态(
PauseState
)可进入播放状态(PlayState
)和停止状态(StopState
)。 - 停止播放状态(
StopState
)点击播放可重新进入播放状态(PlayState
)。
转态
public interface State {
void start();
void pause();
void play();
void stop();
}
准备状态
public class PrepareState implements State {
private MediaPlayer mediaPlayer;
public PrepareState(MediaPlayer mediaPlayer) {
this.mediaPlayer = mediaPlayer;
}
@Override
public void start() {
System.out.println("开始播放视频");
mediaPlayer.setState(mediaPlayer.playState);
}
@Override
public void pause() {
System.out.println("视频还没开始播放");
}
@Override
public void play() {
System.out.println("视频还没开始播放");
}
@Override
public void stop() {
System.out.println("视频还没开始播放");
}
}
播放状态
public class PlayState implements State {
private MediaPlayer mediaPlayer;
public PlayState(MediaPlayer mediaPlayer) {
this.mediaPlayer = mediaPlayer;
}
@Override
public void start() {
System.out.println("视频已经开始播放了");
}
@Override
public void pause() {
System.out.println("视频暂停");
mediaPlayer.setState(mediaPlayer.pauseState);
}
@Override
public void play() {
System.out.println("视频播放中,无效操作");
}
@Override
public void stop() {
System.out.println("视频结束播放");
mediaPlayer.setState(mediaPlayer.stopState);
}
}
暂停状态
public class PauseState implements State {
private MediaPlayer mediaPlayer;
public PauseState(MediaPlayer mediaPlayer) {
this.mediaPlayer = mediaPlayer;
}
@Override
public void start() {
System.out.println("视频已经开始播放了");
}
@Override
public void pause() {
System.out.println("视频暂停中,无效操作");
}
@Override
public void play() {
System.out.println("视频播放");
mediaPlayer.setState(mediaPlayer.playState);
}
@Override
public void stop() {
System.out.println("视频结束播放");
mediaPlayer.setState(mediaPlayer.stopState);
}
}
停止状态
public class StopState implements State {
private MediaPlayer mediaPlayer;
public StopState(MediaPlayer mediaPlayer) {
this.mediaPlayer = mediaPlayer;
}
@Override
public void start() {
System.out.println("视频已经开始播放了");
mediaPlayer.setState(mediaPlayer.playState);
}
@Override
public void pause() {
System.out.println("视频已经停止播放了");
}
@Override
public void play() {
System.out.println("视频已经停止播放了");
}
@Override
public void stop() {
System.out.println("视频停止播放中,无效操作");
}
}
播放器
public class MediaPlayer {
State prepareState;
State playState;
State pauseState;
State stopState;
private State state;
public MediaPlayer() {
prepareState = new PrepareState(this);
playState = new PlayState(this);
pauseState = new PauseState(this);
stopState = new StopState(this);
state = prepareState;
}
public void setState(State state) {
this.state = state;
}
public void start() {
state.start();
}
public void pause() {
state.pause();
}
public void play() {
state.play();
}
public void stop() {
state.stop();
}
}
运行代码
public static void main(String[] args) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.start();
mediaPlayer.pause();
mediaPlayer.play();
mediaPlayer.stop();
}
输出
相关文章
Java 设计模式