一、状态模式的定义

状态模式的关键是区分事务内部和外部的状态,事务内部状态改变往往会带来事务的行为改变。

状态模式中有意思的一点是,一般我们谈到封装,都是优先封装对象的行为,而非对象的状态。但在状态模式中刚好相反,状态模式的关键是把事务的每种状态都封装为单独的类,跟此种状态有关的行为都封装在这个类的内部。与此同时,我们还可以把状态的切换规则实现分布在状态类中,这样就有效消除了原本存在的大量条件分支语句。

二、状态模式的应用案例——文件上传

在现实中,状态模式的应用案例有很多,如文件上传程序有文件扫描、正在上传、暂停、上传成功、上传失败这几种状态,音乐播放器有加载中、正在播放、暂停、播放完毕这几种状态。点击同一个按钮,在上传中和暂停状态下的行为表现是不一样的,同时他们的class也不一样。

在文件上传中,涉及到两个控制按钮:第一个用于暂停和继续上传,第二个用于删除文件;文件在不同的状态下,点击这两个按钮,发生的行为也不同:

1)文件在扫描过程中,是不能进行任何操作的,既不能暂停也不能删除文件,只能等待扫描完成。扫描完成后,根据文件的md5进行判断,如果文件已经存在于服务器中,则直接跳转到上传完成状态。如果文件的大小超过允许上传的最大值,或者文件已经损坏,则跳转到上传失败状态。

2)上传过程中可以点击暂停按钮来暂停上传,暂停后点击同一个按钮会继续上传。

3)扫描和上传过程中,点击删除按钮无效,只有在暂停、上传完成、上传失败之后,才能删除文件。

文件上传是一个异步的过程,在上传过程中,上传插件会不停地调用javascript提供的一个全局函数window.external.upload,来通知javascript目前的上传进度,控件会把当前的文件状态作为参数state塞进window.external.upload.这里使用setTimeout来模拟文件的上传进度,以下代码模拟上传插件的实现:

  1. //上传插件对象
  2. var plugin = (function() {
  3. var plugin = document.createElement('embed');
  4. plugin.style.display = 'none';
  5. plugin.type = 'application/txftn-webkit';
  6. //插件扫描
  7. plugin.sign = function() {
  8. console.log('plugin开始进行文件扫描');
  9. };
  10. plugin.pause = function() {
  11. console.log('plugin暂停文件扫描');
  12. };
  13. plugin.uploading = function() {
  14. console.log('plugin开始文件上传');
  15. };
  16. plugin.del = function() {
  17. console.log('plugin删除文件上传');
  18. };
  19. plugin.done = function() {
  20. console.log('plugin文件上传完成');
  21. };
  22. document.body.appendChild(plugin);
  23. return plugin;
  24. })();

如果编写传统代码实现文件上传,代码如下:

  1. //采用传统方法实现文件上传
  2. //实现上传类
  3. var Upload = function(fileName) {
  4. this.fileName = fileName;
  5. this.plugin = plugin;
  6. this.button1 = null; //控制继续、暂停上传等
  7. this.button2 = null; //控制上传文件删除等
  8. this.curState = 'sign'; //设置初始状态为文件扫描
  9. };
  10. Upload.prototype.init = function() {
  11. this.div = document.createElement('div');
  12. this.div.innerHTML = '<span>文件名称:' + this.fileName + '</span><button id="button1">扫描中</button><button id="button2">删除</button>';
  13. document.body.appendChild(this.div);
  14. this.button1 = document.getElementById('button1');
  15. this.button2 = document.getElementById('button2');
  16. this.bindEvent();
  17. };
  18. Upload.prototype.changeState = function(state) { //负责切换状态的具体行为
  19. this.curState=state;
  20. switch (state) {
  21. case 'sign':
  22. this.plugin.sign();
  23. this.button1.innerHTML = '扫描中,任何操作无效';
  24. break;
  25. case 'uploading':
  26. this.plugin.uploading();
  27. this.button1.innerHTML = '正在上传,点击暂停';
  28. break;
  29. case 'pause':
  30. this.plugin.pause();
  31. this.button1.innerHTML = '已暂停,点击继续上传';
  32. break;
  33. case 'done':
  34. this.plugin.done();
  35. this.button1.innerHTML = '文件上传完成';
  36. break;
  37. case 'error':
  38. this.button1.innerHTML = '文件上传失败';
  39. break;
  40. case 'del':
  41. this.plugin.del();
  42. this.div.parentNode.removeChild(this.div);
  43. console.log('删除完成');
  44. break;
  45. }
  46.  
  47. };
  48. Upload.prototype.bindEvent = function() {
  49. var self = this;
  50. this.button1.onclick = function() {
  51. if (self.curState === 'sign') {
  52. console.log('扫描中,点击无效...');
  53. } else if (self.curState === 'uploading') { //上传中,切换到暂停样式
  54. self.changeState('pause');
  55. } else if (self.curState === 'pause') { //暂停中,切换到上传模式
  56. self.changeState('uploading');
  57. } else if (self.curState === 'done') {
  58. console.log('文件已经上传,点击无效...');
  59. } else if (self.curState === 'error') {
  60. console.log('文件上传失败,点击无效...');
  61. }
  62. };
  63. this.button2.onclick = function() {
  64. if (self.curState === 'done' || self.curState === 'error' || self.curState === 'pause') {
  65. self.changeState('del'); //以上三种状态下可以删除
  66. } else if (self.curState === 'sign') {
  67. console.log('文件正在扫描中,不能删除');
  68. } else if (self.curState === 'uploading') {
  69. console.log('文件正在上传中,不能删除');
  70. }
  71. };
  72. };

上面完成了一个简单的文件上传程序。当然这是一个反例,程序中充斥着大量的if-else语句,状态和行为都被耦合到一个巨大的方法里,难以进行修改和扩展。

测试:

  1. var uploadObj=new Upload('java疯狂讲义');
  2. uploadObj.init();
  3.  
  4. window.external.upload=function(state){
  5. uploadObj.changeState(state);
  6. };
  7. window.external.upload('sign');
  8. setTimeout(function(){
  9. window.external.upload('uploading');
  10. },);
  11. setTimeout(function(){
  12. window.external.upload('done');
  13. },);

运用状态模式重构以上代码:

  1. //上传插件对象
  2. var plugin = (function() {
  3. var plugin = document.createElement('embed');
  4. plugin.style.display = 'none';
  5. plugin.type = 'application/txftn-webkit';
  6. //插件扫描
  7. plugin.sign = function() {
  8. console.log('plugin开始进行文件扫描');
  9. };
  10. plugin.pause = function() {
  11. console.log('plugin暂停文件扫描');
  12. };
  13. plugin.uploading = function() {
  14. console.log('plugin开始文件上传');
  15. };
  16. plugin.del = function() {
  17. console.log('plugin删除文件上传');
  18. };
  19. plugin.done = function() {
  20. console.log('plugin文件上传完成');
  21. };
  22. document.body.appendChild(plugin);
  23. return plugin;
  24. })();
  25.  
  26. //使用状态模式重构文件上传代码
  27. var Upload = function() {
  28. this.plugin = plugin;
  29. this.fileName = fileName;
  30. this.button1 = null;
  31. this.button2 = null;
  32. //构建各个状态类
  33. this.signState = new SignState(this);
  34. this.uploadingState = new UploadingState(this);
  35. this.pauseState = new PauseState(this);
  36. this.doneState = new DoneState(this);
  37. this.errorState = new ErrorState(this);
  38. this.curState = this.signState;
  39. };
  40.  
  41. Upload.prototype.init = function() {
  42. this.div = document.createElement('div');
  43. this.div.innerHTML = '<span>文件名称:' + this.fileName + '</span><button id="button1">扫描中</button><button id="button2">删除</button>';
  44. document.body.appendChild(this.div);
  45. this.button1 = document.getElementById('button1');
  46. this.button2 = document.getElementById('button2');
  47. this.bindEvent();
  48. };
  49. Upload.prototype.bindEvent = function() {
  50. var self = this;
  51. this.button1.onclick = function() {
  52. self.curState.clickHandler1();
  53. };
  54. this.button2.onclick = function() {
  55. self.curState.clickHandler2();
  56. };
  57. };
  58. Upload.prototype.sign=function(){
  59. this.plugin.sign();
  60. this.curState=this.signState;
  61. };
  62. Upload.prototype.uploading=function(){
  63. this.button1.innerHTML='正在上传,点击暂停';
  64. this.plugin.uploading();
  65. this.curState=this.uploadingState;
  66. };
  67. Upload.prototype.pause=function(){
  68. this.button1.innerHTML='已暂停,点击继续上传';
  69. this.plugin.pause();
  70. this.curState=this.pauseState;
  71. };
  72. Upload.prototype.done=function(){
  73. this.button1.innerHTML='上传完成';
  74. this.plugin.done();
  75. this.curState=this.doneState;
  76. };
  77. Upload.prototype.error=function(){
  78. this.button1.innerHTML='上传失败';
  79. this.curState=this.errorState;
  80. };
  81. Upload.prototype.del=function(){
  82. this.plugin.del();
  83. this.div.parentNode.removeChild(this.div);
  84. };
  85.  
  86. //接下来编写各个状态类的实现
  87. var StateFactory = (function() {
  88. //这个相当于抽象类
  89. var State = function() {};
  90. State.prototype.clickHandler1 = function() {
  91. throw new Error('子类必须重写父类的clickHandler1方法');
  92. };
  93. Stat8e.prototype.clickHandler2 = function() {
  94. throw new Error('子类必须重写父类的clickHandler2方法');
  95. };
  96. return function(param) {
  97. var F = function(uploadObj) {
  98. this.uploadObj = uploadObj;
  99. };
  100. F.prototype = new State();
  101. for (var i in param) {
  102. F.prototype[i] = param[i];
  103. }
  104. return F;
  105. };
  106. })();
  107. var signState = StateFactory({
  108. clickHandler1: function() {
  109. console.log('扫描中,点击无效');
  110. },
  111. clickHandler2: function() {
  112. console.log('文件正在扫描,不能删除');
  113. }
  114. });
  115. var UploadingState = StateFactory({
  116. clickHandler1: function() {
  117. this.uploadObj.pause();
  118. },
  119. clickHandler2: function() {
  120. console.log('文件正在上传,不能删除');
  121. }
  122. });
  123. var PauseState = StateFactory({
  124. clickHandler1: function() {
  125. this.uploadObj.uploading();
  126. },
  127. clickHandler2: function() {
  128. this.uploadObj.del();
  129. }
  130. });
  131. var DoneState = StateFactory({
  132. clickHandler1: function() {
  133. console.log('文件上传完成,点击无效');
  134. },
  135. clickHandler2: function() {
  136. this.uploadObj.del();
  137. }
  138. });
  139. var ErrorState = StateFactory({
  140. clickHandler1: function() {
  141. console.log('文件上传失败,点击无效');
  142. },
  143. clickHandler2: function() {
  144. this.uploadObj.del();
  145. }
  146. });
  147. window.external.upload=function(){
  148.  
  149. };

状态模式控制电灯案例: 

下面已切换电灯状态这个相对简单的例子来进一步说明状态模式:

  1. //状态模式学习
  2. //以控制灯泡开关的状态为例,电灯开关开着的时候,按下开关,电灯会切换到关闭状态,反之,则切换到开启状态
  1. var Light=function(){
  2. this.state='off';
  3. this.button=null;
  4. this.stateSpan=document.getElementById('lightState');
  5. };
  6. Light.prototype.init=function(){
  7. var button=document.createElement('button');
  8. button.innerHTML='开关';
  9. this.button=document.body.appendChild(button);
  10. this.bindEvent();
  11. };
  12. Light.prototype.bindEvent=function(){
  13. var self=this;
  14. this.button.onclick=function(e){
  15. self.buttonWasPressed();
  16. };
  17. };
  18.  
  19. Light.prototype.buttonWasPressed=function(){
  20. if(this.state==='off'){
  21. console.log('电灯打开了');
  22. this.stateSpan.innerHTML='打开';
  23. this.state='on';
  24. }else if(this.state==='on'){
  25. this.stateSpan.innerHTML='关闭';
  26. console.log('电灯关闭了');
  27. this.state='off';
  28. }
  29. };
  30.  
  31. //测试
  32. var light=new Light();
  33. light.init();

以上代码中的buttonWasPressed方法显然违背了封闭-开放原则,电灯的状态除了开,关之外,还可能有多种,如强光,弱光等,每一次新增或者修改状态都需要修改buttonWasPressed方法,该方法将变得极为庞大和臃肿,以至于极难维护。

  1. //使用状态模式进行代码重构
  2. //
  3. function OnState(light){
  4. this.light=light;
  5. }
  6. OnState.prototype.buttonWasPressed=function(){
  7. this.light.lightSpan.innerHTML='关闭';
  8. this.light.curState=this.light.offState;
  9. };
  10.  
  11. function OffState(light){
  12. this.light=light;
  13. }
  14. OffState.prototype.buttonWasPressed=function(){
  15. this.light.lightSpan.innerHTML='打开';
  16. this.light.curState=this.light.onState;
  17. };
  18. var Light=function(){
  19. this.button=null;
  20. this.lightSpan=document.getElementById('lightState');
  21. this.onState=new OnState(this);
  22. this.offState=new OffState(this);
  23. this.curState=this.offState;
  24. };
  25. Light.prototype.init=function(){
  26. var button=document.createElement('button');
  27. button.innerHTML='开关';
  28. this.button=document.body.appendChild(button);
  29. var self=this;
  30. this.button.onclick=function(){
  31. self.buttonWasPressed();
  32. };
  33. };
  34. Light.prototype.buttonWasPressed=function(){
  35. this.curState.buttonWasPressed();
  36. };
  37.  
  38. //测试
  39. var light=new Light();
  40. light.init();

由此可见,使用状态模式的好处很明显,可以使得每种状态和它所对应的行为之间的关系局部化,当light需要增添一个新的状态的时候,无需编写过多的if-else语句,只需要新增一个状态类,再稍稍更改现有的代码即可。

同样也可以看出状态模式的缺点,其缺点在于会使得系统中增添很多的状态类。因此系统会增减不少对象,同事由于避开了if-else语句,也造成了逻辑分散的问题,无法再一个地方看出整个状态机的逻辑。

其实,上面的例子里,再javascript这种无类的语言中,没有规定让状态对象一定要从某个类中创建出来,通过Function.prototype.call方法可以直接将请求委托给某个字面量兑现来执行。

  1. var Light=function(){
  2. this.button=null;
  3. this.lightSpan=document.getElementById('lightState');
  4. this.curState=FSM.off;
  5. };
  6.  
  7. var FSM={
  8. 'on':{
  9. 'buttonWasPressed':function(){
  10. this.curState=FSM.off;
  11. this.lightSpan.innerHTML='关闭';
  12. }
  13. },
  14. 'off':{
  15. 'buttonWasPressed':function(){
  16. this.curState=FSM.on;
  17. this.lightSpan.innerHTML='打开';
  18. }
  19. }
  20. };
  21. Light.prototype.init=function(){
  22. var button=document.createElement('button');
  23. button.innerHTML='开关';
  24. var self=this;
  25. this.button=document.body.appendChild(button);
  26. button.onclick=function(){
  27. self.curState.buttonWasPressed.call(self);
  28. };
  29. };
  30.  
  31. var light=new Light();
  32. light.init();

javascript设计模式学习之十六——状态模式的更多相关文章

  1. C#设计模式学习笔记:(18)状态模式

    本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/8032683.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲行为型设计模式的第六个模式--状 ...

  2. Javascript设计模式理论与实战:状态模式

    在软件开发中,很大部分时候就是操作数据,而不同数据下展示的结果我们将其抽象出来称为状态,我们平时开发时本质上就是对应用程序的各种状态进行切换并作出相应处理.状态模式就是一种适合多种状态场景下的设计模式 ...

  3. 前端读者 | Javascript设计模式理论与实战:状态模式

    本文来自 @狼狼的蓝胖子:链接:http://luopq.com/2015/11/25/design-pattern-state/ 在软件开发中,很大部分时候就是操作数据,而不同数据下展示的结果我们将 ...

  4. javascript设计模式学习之十二——享元模式

    一.享元模式的定义及使用场景 享元模式是为了解决性能问题而诞生的设计模式,这和大部分设计模式为了提高程序复用性的原因不太一样,如果系统中因为创建了大量类似对象而导致内存占用过高,享元模式就非常有用了. ...

  5. javascript设计模式学习之十——组合模式

    一.组合模式定义及使用场景 组合模式将对象组合成树形结构,用以表示“部分—整体”的层次结构,除了用来表示树形结构之外,组合模式还可以利用对象的多态性表现,使得用户对单个对象和组合对象的使用具有一致性. ...

  6. javascript设计模式学习之十五——装饰者模式

    一.装饰者模式定义 装饰者模式可以动态地给某个对象添加一些额外的职责,而不会影响从这个类中派生的其他对象.这种为对象动态添加职责的方式就称为装饰者模式.装饰者对象和它所装饰的对象拥有一致的接口,对于用 ...

  7. javascript设计模式学习之十四——中介者模式

    一.中介者模式的定义和应用场景 中介者模式的作用在于解除对象之间的紧耦合关系,增加一个中介者之后,所有对象都通过中介者来通信,而不是互相引用,当一个对象发生变化的时候,仅需要通知中介者即可.从而将网状 ...

  8. javascript设计模式学习之十三——职责链模式

    一.职责链的定义和使用场景 职责链模式的定义是,职责链模式将一系列可能会处理请求的对象连接成一条链,请求在这些对象之间一次传递,直到遇到一个可以处理它的对象.从而避免请求的发送者和接收者之间的耦合关系 ...

  9. JavaScript设计模式学习——builder pattern(建造者模式)

    个人理解的应用场景 举个例子,比如想要创建各种类型的车的实例,车的类型有很多种,但创建每种类型车的接口定义可能是一样的,就用到了此模式 相关概念的通俗解释 上述例子中接口的定义叫builder 接口到 ...

随机推荐

  1. autoLayout 纯代码

    SB中拖好空间,让后分别在,Pin,Align,Resolve Auto Layout Issues三个面板中设置好约束就好了. 用存代码的方式给控件添加约束,完成自动布局: 利用NSLayoutCo ...

  2. 强、软、弱、虚引用,ReferenceQueue,WeakHashMap

    强引用(Reference):所谓强引用就是普通引用.普通引用引用的对象,即使内存不足时,一般情况下也不会被回收. 软引用(weakReference):如果对象被且仅被软引用所引用时,内存不足时,会 ...

  3. FZU 2092 bfs+记忆化搜索

    晚上团队训练赛的题 和普通bfs不同的是 这是同时操纵人与影子两个单位进行的bfs 由于可能发生人和影子同时接触水晶 所以不可以分开操作 当时使用node记录人和影子的位置 然后进行两重for循环来分 ...

  4. 一个项目软件的大小基本都占用在外部引用的jar包上了。

    1.一个项目几百兆,基本都是外部jar包,引用的. 2.自己本身业务代码并没有那么多的 3.看下meven的仓库大小就知道了,都几百兆

  5. centos时间同步方法

    centos时间同步方法 电脑软硬件应用网 45IT.COM 时间:2012-12-08 18:09 作者:李本清 新装的服务器可能设置了错误的,需要调整时区并调整时间.如下是使用NTP来从一个时间服 ...

  6. 微信自定义菜单说php json_encode不转义中文汉字的方法

    http://blog.csdn.net/qmhball/article/details/45690017 最近在开发微信自定义菜单. 接口比较简单,就是按微信要求的格式post一段json数据过去就 ...

  7. Natural Language Toolkit

    http://www.nltk.org/ >>> import nltk >>> nltk.download()

  8. Java中同步

    解决资源共享的同步操作,有两种方法:一是同步代码块,二是同步方法. 在需要同步的代码块加上synchronized关键字, 同步代码块时必须指定一个需要同步的对象,但一般都是将当前对象(this)设置 ...

  9. oracle 嵌套表

    --自定义对象 CREATE OR REPLACE TYPE Fas_checksheetinfo_line_obj AS OBJECT(  CSID_ID           VARCHAR2(32 ...

  10. Xcode Shortcuts

    Description:⌘: Command     ⌥: Option     ⌃: Control    ←↑↓→: Left, Up, Down, Right                  ...