<html>
<head>
<link type="text/css" href="https://www.cnblogs.com/css/style.css" />
<title>State Pattern</title>
<script type="text/javascript" src="https://www.jb51.net/jquery.js"></script>
<script type="text/javascript" src="https://www.jb51.net/Download.js"></script>
<script type="text/javascript" src="https://www.jb51.net/states/State.js"></script>
<script type="text/javascript" src="https://www.jb51.net/states/DownloadFailedState.js"></script>
<script type="text/javascript" src="https://www.jb51.net/states/DownloadPausedState.js"></script>
<script type="text/javascript" src="https://www.jb51.net/states/DownloadedState.js"></script>
<script type="text/javascript" src="https://www.jb51.net/states/DownloadingState.js"></script>
<script type="text/javascript" src="https://www.jb51.net/states/ReadyState.js"></script>
</head>
<body>
<input type="button" value="开始下载" />
<input type="button" value="暂停" />
<input type="button" value="重新下载" />
<script type="text/javascript">
var oDownload = new Download();
$("#download_button").click(function () {
oDownload.download();
});
$("#pause_button").click(function () {
oDownload.pause();
});
$("#resume_button").click(function () {
oDownload.download();
});
</script>
</body>
</html>
总结
状态模式的使用场景也特别明确,有如下两点:
1.一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为。
2.一个操作中含有大量的分支语句,而且这些分支语句依赖于该对象的状态。状态通常为一个或多个枚举常量的表示。
您可能感兴趣的文章: