博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
状态模式
阅读量:5155 次
发布时间:2019-06-13

本文共 5077 字,大约阅读时间需要 16 分钟。

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     @Override10     public void speed() {11         super.courseVideoContext.setCourseVideoState(CourseVideoContext.SPEED_STATE);12     }13 14     @Override15     public void pause() {16         super.courseVideoContext.setCourseVideoState(CourseVideoContext.PAUSE_STATE);17     }18 19     @Override20     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     @Override10     public void speed() {11         System.out.println("快进播放课程视频状态");12     }13 14     @Override15     public void pause() {16         super.courseVideoContext.setCourseVideoState(CourseVideoContext.PAUSE_STATE);17     }18 19     @Override20     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     @Override10     public void speed() {11         super.courseVideoContext.setCourseVideoState(CourseVideoContext.SPEED_STATE);12     }13 14     @Override15     public void pause() {16         System.out.println("暂停播放课程视频状态");17     }18 19     @Override20     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     @Override10     public void speed() {11         System.out.println("ERROR 停止状态不能快进!!");12     }13 14     @Override15     public void pause() {16         System.out.println("ERROR 停止状态不能暂停!!");17     }18 19     @Override20     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 }

 

转载于:https://www.cnblogs.com/linbq1911/p/10231137.html

你可能感兴趣的文章
GOIP connects with Elastix through “config by line”
查看>>
修改Win7远程桌面端口
查看>>
关于多线程的死锁
查看>>
2018-2019-1 20165205 20165233 实验二 固件程序设计
查看>>
APUE 学习笔记(三) 文件和目录
查看>>
APUE 学习笔记(十) 高级I/O
查看>>
Android Monkey压力测试
查看>>
BZOJ1878: [SDOI2009]HH的项链
查看>>
关于程序、进程和线程
查看>>
android登陆自动调整代码
查看>>
datagridview的数据存取
查看>>
解决Python3 pip list 红色DEPRECATION
查看>>
Linux SSH使用公钥私钥实现免登陆 以及 登陆失败、公钥失效的处理<转>
查看>>
Oracle11g手动创建数据库方法
查看>>
编译Linux内核
查看>>
iOS从生成证书到打包上架-02(详细2016-10最新)
查看>>
iOS 关于内购
查看>>
vs2013 插件
查看>>
CSS3 box-shadow实现纸张的曲线投影效果
查看>>
浏览器内容双缓冲的设想
查看>>