我需要在Java中实现播放和录制视频的应用程序。
视频播放器有两种状态:Playing mode
和recording mode
。
在播放模式下,我们可以Play, Pause,Stop, Forward
和Backward
视频
另一方面,在录制模式下,我们可以Record, Pause
或Stop
这是我到目前为止所做的: br>
我有点卡住了。我不知道我是否应该为每种模式的每个动作创建一个策略,或者我是否可以使它更简单。
我在考虑给PlayingModeVideoPlayer
和RecordingModeVideoPlayer
他们的specefic策略(多个),但我不知道如何实现它。
谢谢你播放和录制模式是两个独立的事情。我不需要实现这两个实体之间的过渡。
答案 0 :(得分:1)
注意:从OP来看,目前尚不清楚它是否是一个数字视频播放器,它会根据当前模式(播放或录制)自动更改其UI,或者它是一个老式的视频带键的播放器(这意味着用户可以在播放模式时点击录音)。
所以,这只是一个可以帮助你前进的想法。
VideoPlayer
界面允许用户开始播放或录制。当选择其中一个时,用户将被移交给指定可能的操作的不同界面(合同)。
public interface VideoPlayer {
PlayerContext play();
RecorderContext record();
}
public class PlayerContext {
private VideoPlayer videoPlayer;
private PlayerState playerState;
public PlayerContext(VideoPlayer videoPlayer) {
this.videoPlayer = videoPlayer;
//this.playerState = ..init with PlayingState..
}
PlayerContext play() {
playerState.play();
return this;
}
PlayerContext pause() {
playerState.pause();
return this;
}
VideoPlayer stop() {
playerState.stop();
return videoPlayer;
}
PlayerContext forward() {
playerState.forward();
return this;
}
PlayerContext backward() {
playerState.backward();
return this;
}
//Methods to enable state switching
}
public class RecorderContext {
private VideoPlayer videoPlayer;
private RecorderState recorderState;
public RecorderContext(VideoPlayer videoPlayer) {
this.videoPlayer = videoPlayer;
//this.recorderState = ..init with record state
}
RecorderContext pause() {
recorderState.pause();
return this;
}
RecorderContext record() {
recorderState.record();
return this;
}
VideoPlayer stop() {
recorderState.stop();
return videoPlayer;
}
//Methods to enable state switching
}
当点击stop
时,用户可以返回并选择两个可用选项之一 - 即,e可以播放或录制。这就是stop
返回基础VideoPlayer
对象的原因。
public class ConcreteVideoPlayer implements VideoPlayer {
@Override
public PlayerContext play() {
return new PlayerContext(this);
}
@Override
public RecorderContext record() {
return new RecorderContext(this);
}
}
public interface PlayerState {
void play();
void pause();
VideoPlayer stop();
void forward();
void backward();
}
public interface RecorderState {
void pause();
void record();
VideoPlayer stop();
}
您可以自己实现每个州的实施(PlayerState
和RecorderState
)。
<强>用法:强>
VideoPlayer videoPlayer = new ConcreteVideoPlayer();
PlayerContext playerContext = videoPlayer.play();
videoPlayer = playerContext.pause()
.backward()
.pause()
.play() //.play() -> Invalid state calls have to be handled by the individual state implementations
.stop();
RecorderContext recordContext = videoPlayer.record();
videoPlayer = recordContext.pause()
.record()
.pause()
.record()
.stop();
我希望这会有所帮助!!