编程基础设计模式状态模式_第1页
编程基础设计模式状态模式_第2页
编程基础设计模式状态模式_第3页
已阅读5页,还剩2页未读, 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、介绍状态模式(Se)允许一个对象在其状态改变的时候改变它的行为,对象看起来似乎修改了它的类。正文举个例子,就比如(ReadySe)、平时在东西,通常就会有好几个状态,比如准备状态状态(DownloadingSe)、暂停状态(DownloausedSe)、完毕状态(DownloadedSe)、失败状态(DownloadFailedSe),也就是说在每个状态都只可以做当前状态才可以做的事情,而不能做其它状态能做的事儿。由于 S式的关键e 模式描述了(Download)如何在每一种状态下不同的行为。这一模就是引入了一个叫做 Se 的抽象类(或 JS 里的函数)来表示状态,Se 函数(作为原型)为每个

2、状态的子类(继承函数)了一些公共接口。其每个继承函数实现与特定状态相关的行为,比如 DownloadingSe 和 DownloadedSe 分别实现完毕的行为。这些行为可以通过 Download 来来了正在和。让来实现一把,首先定义作为其他基础函数的原型的 Se 函数:varSe = function () ;Stotype.download = function () throw new Error(该方法必须被重载!);Stotype.pause = function () throw new Error(该方法必须被重载!);Stotype.fail =

3、function () throw new Error(该方法必须被重载!);Stotype.finish = function () throw new Error(该方法必须被重载!);为 Se 的原型定义了 4 个方法接口,分别对应着(download)、暂停(pause)、失败(fail)、结束(finish)以便子函数可以重写。在编写子函数之前,先来编写一个 ReadySe 函数,以便可以将状态传递给第一个download 状态:var ReadySe = function (oDownload) Se.apply(this);this.oDownload = oDownl

4、oad;ReadyStotype = new Se();ReadyStotype.download = function () this.oDownload.setSe(this.oDownload.getDownloadingSe();/ Ready 以后,可以开始,所以设置了 Download 函数里的状态获取方法console.log(Start Download!);ReadyStotype.pause = function () throw new Error(还没开始,不能暂停!);ReadyStotype.fail = function (

5、) throw new Error(文件还没开始,怎么能说失败呢!);ReadyStotype.finish = function () throw new Error(文件还没开始,当然也不能结束了!);该函数接收了一个 Download 函数的实例作为参数,Download 函数用于控制状态的改变和获取(类似于 控制器,让外部调用),ReadyS e 重写了原型的 download 方法,以便开始进行 。 继续来看 Download 函数的主要功能:var Download = function () this.oSe = new ReadySe(this);Downloroto

6、type.setSe = function (oSe) this.oSe = oSe;/ 对外的四个公共方法,以便外部调用Downlorototype.download = function () this.oSe.download();Downlorototype.pause = function () this.oSe.pause();Downlorototype.fail = function () this.oSe.fail();Downlorototype.finish = function () this.oSe.finish();/获取各种状态,传入当前 this 对象Downl

7、orototype.getReadySe = function () return new ReadySe(this);Downlorototype.getDownloadingSe = function () return new DownloadingSe(this);Downlorototype.getDownloausedSe = function () return new DownloausedSe(this);Downlorototype.getDownloadedSe = function () return new DownloadedSe(this);Downlorotot

8、ype.getDownloadedFailedSe = function () return new DownloadFailedSe(this);Download 函数的原型提供了 8 个方法,4 个是对用于状态的操作行为,另外 4 个是用于获取当前四个不同的状态,这 4 个方法都接收 this 作为参数,也就是将 Download 实例自身作为一个参数传递给处理该请求的状态对象(ReadySe 以及后面要实现的继承函数),这使得状态对象比必要的时候可以oDownlaod。接下来,继续定义 4 个相关状态的函数:var DownloadingSe = function (oDownload)

9、 Se.apply(this);this.oDownload = oDownload;DownloadingStotype = new Se(); DownloadingStotype.download = function () throw new Error(文件已经正在中了!);DownloadingStotype.pause = function () this.oDownload.setSe(this.oDownload.getDownloausedSe(); console.log(暂停!);DownloadingStotype.fail =

10、 function () this.oDownload.setSe(this.oDownload.getDownloadedFailedSe(); console.log(失败!);DownloadingStotype.finish = function () this.oDownload.setSe(this.oDownload.getDownloadedSe(); console.log(完毕!);DownloadingSe 的主要注意事项就是已经正在的文件,不能再次开始了,其它的状态都可以连续进行。var DownloausedSe = function (oDownload)

11、 Se.apply(this);this.oDownload = oDownload;DownloausedStotype = new Se();DownloausedStotype.download = function () this.oDownload.setSe(this.oDownload.getDownloadingSe(); console.log(继续!);DownloausedStotype.pause = function () throw new Error(已经暂停了,咋还要暂停呢!);DownloausedStotype.fai

12、l = function () this.oDownload.setSe(this.oDownload.getDownloadedFailedSe(); console.log(失败!);DownloausedStotype.finish = function () this.oDownload.setSe(this.oDownload.getDownloadedSe(); console.log(完毕!);DownloausedSe 函数里要注意的是,已经暂停的,不能再次暂停。var DownloadedSe = function (oDownload) Se.apply(this

13、);this.oDownload = oDownload;DownloadedStotype = new Se(); DownloadedStotype.download = function () this.oDownload.setSe(this.oDownload.getDownloadingSe(); console.log(重新!);DownloadedStotype.pause = function () throw new Error(对完了,还暂停啥?);DownloadedStotype.fail = function () throw

14、 new Error(都成功了,咋会失败呢?);DownloadedStotype.finish = function () throw new Error(成功了,不能再为成功了吧!);DownloadedSe 函数,同理成功以后,不能再设置 finish 了,只能设置重新状态。var DownloadFailedSe = function (oDownload) Se.apply(this);this.oDownload = oDownload;DownloadFailedStotype = new Se();DownloadFailedStotype.dow

15、nload = function () this.oDownload.setSe(this.oDownload.getDownloadingSe(); console.log(尝试重新!);DownloadFailedStotype.pause = function () throw new Error(失败的,也不能暂停!);DownloadFailedStotype.fail = function () throw new Error(都失败了,咋还失败呢!);DownloadFailedStotype.finish = function () throw n

16、ew Error(失败的,肯定也不会成功!);同理,DownloadFailedSe 函数状态,也不能再次失败,但可以和finished 以后再次尝试重新。调用测试代码,就非常简单了,在 HTML 里演示吧,首先是要了 jquery,然后有 3 个按钮分别代表:开始、暂停、重新。(注意在 Firefox 里用firebug 查看结果,因为用了 console.log 方法)。Se Pattern Javascript/blob/master/Se/1/index.html参考:/tcorral/Design-Patterns-in-一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为。一个操作中的分支语句,而且这些分支语句

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论