码迷,mamicode.com
首页 > 其他好文 > 详细

状态模式

时间:2019-01-07 01:31:28      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:att   场景   bst   状态改变   暂停   dstat   abstract   代码   视频   

1.定义:允许一个对象在其内部状态改变时,改变它的行为。

2.类型:行为型

3.适用场景:一个对象存在多个状态(不同状态下行为不同),且状态可相互转换。

4.优点:将不同的状态隔离,把各种状态的转换逻辑,分布到State的子类中,减少相互间依赖;
      增加新的状态非常简单。

5.缺点:状态多的业务场景导致类数目增加,系统变得复杂

6.相关设计模式:享元模式

7.实例目录package

技术分享图片

8.实例UML类图

技术分享图片

9.代码

 1 package com.geely.design.pattern.behavioral.state;
 2 
 3 public abstract class CourseVideoState {
 4     protected CourseVideoContext courseVideoContext;
 5 
 6     public void setCourseVideoContext(CourseVideoContext courseVideoContext) {
 7         this.courseVideoContext = courseVideoContext;
 8     }
 9 
10     public abstract void play();
11     public abstract void speed();
12     public abstract void pause();
13     public abstract void stop();
14 }
 1 package com.geely.design.pattern.behavioral.state;
 2 
 3 public class PlayState extends CourseVideoState {
 4     @Override
 5     public void play() {
 6         System.out.println("正常播放课程视频状态");
 7     }
 8 
 9     @Override
10     public void speed() {
11         super.courseVideoContext.setCourseVideoState(CourseVideoContext.SPEED_STATE);
12     }
13 
14     @Override
15     public void pause() {
16         super.courseVideoContext.setCourseVideoState(CourseVideoContext.PAUSE_STATE);
17     }
18 
19     @Override
20     public void stop() {
21         super.courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE);
22     }
23 }
 1 package com.geely.design.pattern.behavioral.state;
 2 
 3 public class SpeedState extends CourseVideoState {
 4     @Override
 5     public void play() {
 6         super.courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE);
 7     }
 8 
 9     @Override
10     public void speed() {
11         System.out.println("快进播放课程视频状态");
12     }
13 
14     @Override
15     public void pause() {
16         super.courseVideoContext.setCourseVideoState(CourseVideoContext.PAUSE_STATE);
17     }
18 
19     @Override
20     public void stop() {
21         super.courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE);
22     }
23 }
 1 package com.geely.design.pattern.behavioral.state;
 2 
 3 public class PauseState extends CourseVideoState {
 4     @Override
 5     public void play() {
 6         super.courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE);
 7     }
 8 
 9     @Override
10     public void speed() {
11         super.courseVideoContext.setCourseVideoState(CourseVideoContext.SPEED_STATE);
12     }
13 
14     @Override
15     public void pause() {
16         System.out.println("暂停播放课程视频状态");
17     }
18 
19     @Override
20     public void stop() {
21         super.courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE);
22     }
23 }
 1 package com.geely.design.pattern.behavioral.state;
 2 
 3 public class StopState extends CourseVideoState {
 4     @Override
 5     public void play() {
 6         super.courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE);
 7     }
 8 
 9     @Override
10     public void speed() {
11         System.out.println("ERROR 停止状态不能快进!!");
12     }
13 
14     @Override
15     public void pause() {
16         System.out.println("ERROR 停止状态不能暂停!!");
17     }
18 
19     @Override
20     public void stop() {
21         System.out.println("停止播放课程视频状态");
22     }
23 }
 1 package com.geely.design.pattern.behavioral.state;
 2 
 3 public class CourseVideoContext {
 4     private CourseVideoState courseVideoState;
 5 
 6     public final static PlayState PLAY_STATE = new PlayState();
 7     public final static SpeedState SPEED_STATE = new SpeedState();
 8     public final static PauseState PAUSE_STATE = new PauseState();
 9     public final static StopState STOP_STATE = new StopState();
10 
11     public CourseVideoState getCourseVideoState() {
12         return courseVideoState;
13     }
14 
15     public void setCourseVideoState(CourseVideoState courseVideoState) {
16         this.courseVideoState = courseVideoState;
17         this.courseVideoState.setCourseVideoContext(this);
18     }
19 
20     public void play(){
21         this.courseVideoState.play();
22     }
23     public void speed(){
24         this.courseVideoState.speed();
25     }
26     public void stop(){
27         this.courseVideoState.stop();
28     }
29     public void pause(){
30         this.courseVideoState.pause();
31     }
32 }
 1 package com.geely.design.pattern.behavioral.state;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         CourseVideoContext courseVideoContext = new CourseVideoContext();
 6         courseVideoContext.setCourseVideoState(new PlayState());
 7 
 8         System.out.println("当前状态:" + courseVideoContext.getCourseVideoState().getClass().getSimpleName());
 9 
10         courseVideoContext.pause();
11         System.out.println("当前状态:" + courseVideoContext.getCourseVideoState().getClass().getSimpleName());
12 
13         courseVideoContext.speed();
14         System.out.println("当前状态:" + courseVideoContext.getCourseVideoState().getClass().getSimpleName());
15 
16         courseVideoContext.stop();
17         System.out.println("当前状态:" + courseVideoContext.getCourseVideoState().getClass().getSimpleName());
18 
19         courseVideoContext.speed();
20     }
21 }

 

状态模式

标签:att   场景   bst   状态改变   暂停   dstat   abstract   代码   视频   

原文地址:https://www.cnblogs.com/linbq1911/p/10231137.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!