The project source address: https: //github.com/ggb2312/JavaNotes/tree/master/design-pattern (design patterns relevant code and notes)
1. Definitions
It allows an object to change its internal state, to alter its behavior
2. Applicable scene
A plurality of state objects exist (different behavior under different conditions), and the state can be converted to each other
for example: electricity supplier order status torsion
3. The class diagram and role
The Context : It is the object containing the state, it can handle some of the requests, which will eventually produce a response state associated with.
State : state of the interface, which defines the behavior of each state of the collection, these actions will be in use in Context.
ConcreteState : specific state, to achieve specific state-related class actions.
4. The relevant design patterns
Flyweight state mode and
state mode and can be used in combination Flyweight
Example 5. Mode
Background: The player state of the video switch
(1) Video Status abstract class
/**
* Create by lastwhisper on 2019/2/11
*/
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();
}
(2) Video context
/**
* Create by lastwhisper on 2019/2/11
*/
public class CourseVideoContext {
private CourseVideoState courseVideoState;
public final static PlayState PLAY_STATE = new PlayState();
public final static StopState STOP_STATE = new StopState();
public final static PauseState PAUSE_STATE = new PauseState();
public final static SpeedState SPEED_STATE = new SpeedState();
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();
}
}
(3) the state of various specific class of video
PlayState normal course video playback state
/**
* Create by lastwhisper on 2019/2/11
*/
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);
}
}
SpeedState fast-forward the video state curriculum
/**
* Create by lastwhisper on 2019/2/11
*/
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);
}
}
PauseState pause video playback state curriculum
/**
* Create by lastwhisper on 2019/2/11
*/
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);
}
}
StopState stop playing video course status
/**
* Create by lastwhisper on 2019/2/11
*/
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("停止播放课程视频状态");
}
}
(4) test
/**
* Create by lastwhisper on 2019/2/11
*/
public class Test {
public static void main(String[] args){
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();
}
}
Test Results:
(5) FIG class
6. pros and cons
advantage:
- The different states isolation
- Converting various logic states, the subclass distribution of the State, the reduction of mutual dependence
- Very simple to add new state
Disadvantages:
- Multi-state business scenario leads to an increase the number of classes, the system becomes complicated
7. Extended -JDK1.7 source mode and a frame in a state
javax.faces.lifecycle.Lifecycle、javax.faces.webapp.FacesServlet