状态和行为:

所谓对象的状态,通常指的就是对象实例的属性的值;而行为指的就是对象的功能,再具体点说,行为大多可以对应到方法上。

状态模式的功能就是分离状态的行为,通过维护状态的变化,来调用不同状态对应的不同功能。也就是说,状态和行为是相关联的,它们的关系可以描述为:状态决定行为。

由于状态是在运行期被改变的,因此行为也会在运行期根据状态的改变而改变。

环境和状态处理对象:

在状态模式中,环境(Context)是持有状态的对象,但是环境(Context)自身并不处理跟状态相关的行为,而是把处理状态的功能委托给了状态对应的状态处理类来处理。

在具体的状态处理类中经常需要获取环境(Context)自身的数据,甚至在必要的时候会回调环境(Context)的方法,因此,通常将环境(Context)自身当作一个参数传递给具体的状态处理类。

看一个具体的实例:

1.State抽象类(当做接口):

var State = function () {

};

State.prototype.download = function () {
throw new Error("该方法必须被重载!");
}; State.prototype.pause = function () {
throw new Error("该方法必须被重载!");
}; State.prototype.fail = function () {
throw new Error("该方法必须被重载!");
}; State.prototype.finish = function () {
throw new Error("该方法必须被重载!");
};

2.Download类:(所谓的环境类,存有状态属性)

var Download = function () {
this.oState = new ReadyState(this);
}; Download.prototype.setState = function (oState) {
this.oState = oState;
}; // 对外暴露的四个公共方法,以便外部调用 Download.prototype.download = function () {
this.oState.download();
}; Download.prototype.pause = function () {
this.oState.pause();
}; Download.prototype.fail = function () {
this.oState.fail();
}; Download.prototype.finish = function () {
this.oState.finish();
};

3.6种不同的状态(每种状态都会调用Download类的改变状态的方法,进行状态切换)

(1)准备状态:

var ReadyState = function (oDownload) {
State.apply(this);
this.oDownload = oDownload;
}; ReadyState.prototype = new State(); ReadyState.prototype.download = function () {
this.oDownload.setState(this.oDownload.getDownloadingState());
// Ready以后,可以开始下载,所以设置了Download函数里的状态获取方法
console.log("Start Download!");
}; ReadyState.prototype.pause = function () {
throw new Error("还没开始下载,不能暂停!");
}; ReadyState.prototype.fail = function () {
throw new Error("文件还没开始下载,怎么能说失败呢!");
}; ReadyState.prototype.finish = function () {
throw new Error("文件还没开始下载,当然也不能结束了!");
};

(2)

var DownloadingState = function (oDownload) {
State.apply(this);
this.oDownload = oDownload;
}; DownloadingState.prototype = new State(); DownloadingState.prototype.download = function () {
throw new Error("文件已经正在下载中了!");
}; DownloadingState.prototype.pause = function () { this.oDownload.setState(this.oDownload.getDownloadPausedState());
console.log("暂停下载!");
}; DownloadingState.prototype.fail = function () { this.oDownload.setState(this.oDownload.getDownloadedFailedState());
console.log("下载失败!");
}; DownloadingState.prototype.finish = function () {
this.oDownload.setState(this.oDownload.getDownloadedState());
console.log("下载完毕!");
};

  

(3)

var DownloadPausedState = function (oDownload) {
State.apply(this);
this.oDownload = oDownload;
}; DownloadPausedState.prototype = new State(); DownloadPausedState.prototype.download = function () {
this.oDownload.setState(this.oDownload.getDownloadingState());
console.log("继续下载!");
}; DownloadPausedState.prototype.pause = function () {
throw new Error("已经暂停了,咋还要暂停呢!");
}; DownloadPausedState.prototype.fail = function () { this.oDownload.setState(this.oDownload.getDownloadedFailedState());
console.log("下载失败!");
}; DownloadPausedState.prototype.finish = function () {
this.oDownload.setState(this.oDownload.getDownloadedState());
console.log("下载完毕!");
};

  

(4)

var DownloadedState = function (oDownload) {
State.apply(this);
this.oDownload = oDownload;
}; DownloadedState.prototype = new State(); DownloadedState.prototype.download = function () {
this.oDownload.setState(this.oDownload.getDownloadingState());
console.log("重新下载!");
}; DownloadedState.prototype.pause = function () {
throw new Error("对下载完了,还暂停啥?");
}; DownloadedState.prototype.fail = function () {
throw new Error("都下载成功了,咋会失败呢?");
}; DownloadedState.prototype.finish = function () {
throw new Error("下载成功了,不能再为成功了吧!");
};

(5)

var DownloadFailedState = function (oDownload) {
State.apply(this);
this.oDownload = oDownload;
}; DownloadFailedState.prototype = new State(); DownloadFailedState.prototype.download = function () {
this.oDownload.setState(this.oDownload.getDownloadingState());
console.log("尝试重新下载!");
}; DownloadFailedState.prototype.pause = function () {
throw new Error("失败的下载,也不能暂停!");
}; DownloadFailedState.prototype.fail = function () {
throw new Error("都失败了,咋还失败呢!");
}; DownloadFailedState.prototype.finish = function () {
throw new Error("失败的下载,肯定也不会成功!");
};

4.首页:

<body>
<input type="button" value="开始下载" id="download_button" />
<input type="button" value="暂停" id="pause_button" />
<input type="button" value="重新下载" id="resume_button" />
<script> var oDownload = new Download();
$("#download_button").click(function () {
oDownload.download();
}); $("#pause_button").click(function () {
oDownload.pause();
}); $("#resume_button").click(function () {
oDownload.download();
});
</script>
</body>

  

  

  

JavaScript设计模式——状态模式的更多相关文章

  1. JavaScript设计模式 - 状态模式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. javascript设计模式--状态模式(State)

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 【设计模式】Javascript设计模式——状态模式(行为型)

    注:这个模式是非常聪明的,很有点数学中组合的意思,现在,来看下这个模式是怎么个思想. 问题提出:假如某个操作有三种可能,分别为1,2,3,还可能是组合,比如先执行1,再执行2或者先执行2再执行3或者1 ...

  4. 14. 星际争霸之php设计模式--状态模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  5. JavaScript设计模式 - 代理模式

    代理模式是为一个对象提供一个代用品或占位符,以便控制对它的访问 代理模式的用处(个人理解):为了保障当前对象的单一职责(相对独立性),而需要创建另一个对象来处理调用当前对象之前的一些逻辑以提高代码的效 ...

  6. [Head First设计模式]生活中学设计模式——状态模式

    系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...

  7. javascript 设计模式-----策略模式

    在<javascript设计模式>中,作者并没有向我们介绍策略模式,然而它却是一种在开发中十分常见的设计模式.最常见的就是当我们遇到一个复杂的表单验证的时候,常常需要编写一大段的if和el ...

  8. JAVA 设计模式 状态模式

    用途 状态模式 (State) 当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. 状态模式是一种行为型模式. 结构

  9. 深入浅出设计模式——状态模式(State Pattern)

    模式动机 在很多情况下,一个对象的行为取决于一个或多个动态变化的属性,这样的属性叫做状态,这样的对象叫做有状态的 (stateful)对象,这样的对象状态是从事先定义好的一系列值中取出的.当一个这样的 ...

随机推荐

  1. suse11 sp2 搭建openvpn

    什么是VPN IP机制仿真出一个私有的广域网"是通过私有的隧道技术在公共数据网络上仿真一条点到点的专线技术.所谓虚拟,是指用户不再需要拥有实际的长途数据线路,而是使用Internet公众数据 ...

  2. codeforces 460D Little Victor and Set(构造、枚举)

    最近的CF几乎都没打,感觉挺水的一个题,不过自己仿佛状态不在,看题解才知道做法. 输入l, r, k (1 ≤ l ≤ r ≤ 1012; 1 ≤ k ≤ min(106, r - l + 1)). ...

  3. Divide and conquer:Drying(POJ 3104)

    烘干衣服 题目大意:主人公有一个烘干机,但是一次只能烘干一件衣服,每分钟失水k个单位的水量,自然烘干每分钟失水1个单位的水量(在烘干机不算自然烘干的那一个单位的水量),问你最少需要多长时间烘干衣服? ...

  4. java classpath批量设置shell脚本

    java classpath批量设置shell脚本   注意:linux bash jar文件之间的分隔符是':'    export JAR_HOME=path to directory which ...

  5. 【leetcode】Remove Duplicates from Sorted List II (middle)

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  6. IOS- DocumentInteraction Controllerl的使用

    iOS提供了使用其他app预览文件的支持,这就是Document Interaction Controller.此外,iOS也支持文件关联,允许其他程序调用你的app打开某种文件.而且,从4.2开始, ...

  7. PHP安全编程:对输出要进行转义

    为了区分数据是否已转义,我还是建议定义一个命名机制.对于输出到客户机的转义数据,我使用$html数组进行存储,该数据首先初始化成一个空数组,对所有已过滤和已转义数据进行保存. 1 <?php 2 ...

  8. cell分割线宽度不满屏处理

    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZ ...

  9. Linux Shell常用快捷键

    ctrl+a[A]:将光标移到命令行开头 ctrl+e[E]:将光标移到命令行结尾 ctrl+c[C]:强制终止命令执行 ctrl+u[U]:删除/剪切光标之前的所有字符 ctrl+y[Y]:粘贴ct ...

  10. adb device 调试

    1. kill掉占用adb端口的进程 netstat -aon|findstr "5037" TCP 127.0.0.1:5037 0.0.0.0:0 LISTENING 6540 ...